I read somewhere that the best way to learn a programming language, and to get better at coding is to work on projects. Open-source projects can be daunting for beginners so, it’s probably a good idea to roll your own apps for things you need personally. I really wanted to write my own blog but like I mentioned earlier, I set a deadline for the new moon/solar eclipse to launch, and I just couldn’t quite get the “pagination” working in time.
I got the idea to make a Blog for flask newbies. The idea was based on an old ruby-on-rails site I used to visit when I was trying to learn Ruby on Rails. The domain that I have purchased is flaskcast.com and basically all I’m going to do is write a simple blog website in flask that will display the YouTube videos embedded in the posts with a title and date posted. I may eventually add a discussion section for each video.
- Python 3.6.2
- Database (probably use MongoDB, as I am enjoying learning new stuff but may go with sqlite3 or postgres)
- The Flask-MongoAlchemy extension. https://pythonhosted.org/Flask-MongoAlchemy/
- Blueprints (to make the app modular, may need to scale it up at some point)
- Digital Ocean for hosting
- probably more…
The big issue I faced trying to write my own blog engine was that I had to write my own database module that utilized PyMongo. It was a learning experience, and in fact I got most of the code from a Udemy course that I had taken. I’m just not advanced enough right now to write my own pagination code that uses MongoDB. After looking at the docs for the MongoAlchemy extension, it looks like 90% of the code I had to write is already taken care of. This is copied from the docs:
[pastacode lang=”python” manual=”from%20flask%20import%20Flask%0Afrom%20flask.ext.mongoalchemy%20import%20MongoAlchemy%0Aapp%20%3D%20Flask(__name__)%0Aapp.config%5B’MONGOALCHEMY_DATABASE’%5D%20%3D%20’library’%0Adb%20%3D%20MongoAlchemy(app)%0A%0Aclass%20Author(db.Document)%3A%0A%20%20%20%20name%20%3D%20db.StringField()%0A%0Aclass%20Book(db.Document)%3A%0A%20%20%20%20title%20%3D%20db.StringField()%0A%20%20%20%20author%20%3D%20db.DocumentField(Author)%0A%20%20%20%20year%20%3D%20db.IntField()%0A%0Afrom%20application%20import%20Author%2C%20Book%0Amark_pilgrim%20%3D%20Author(name%3D’Mark%20Pilgrim’)%0Adive%20%3D%20Book(title%3D’Dive%20Into%20Python’%2C%20author%3Dmark_pilgrim%2C%20year%3D2004)%0A%0Amark_pilgrim.name%20%3D%20’Mark%20Stalone’%0Amark_pilgrim.save()” message=”Flask-MongoAlchemy example” highlight=”” provider=”manual”/]
So as you can see, basically you create document objects and work with those. Looks like querying the database is really simple as well. My module required some pretty complicated RegEx that I am not comfortable writing without assistance.
Pagination appears to be pretty simple as well…
[pastacode lang=”python” manual=”paginate(page%2C%20per_page%3D20%2C%20error_out%3DTrue)” message=”” highlight=”” provider=”manual”/]
Next post I will share some design ideas.
See you tomorrow!