in Flask, PYTHON, Web Development

Writing a screencast blog with Flask

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.

Tools
  • Python 3.6.2
  • Flask
  • 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
  • PyCharm
  • 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:

from flask import Flask
from flask.ext.mongoalchemy import MongoAlchemy
app = Flask(__name__)
app.config['MONGOALCHEMY_DATABASE'] = 'library'
db = MongoAlchemy(app)

class Author(db.Document):
    name = db.StringField()

class Book(db.Document):
    title = db.StringField()
    author = db.DocumentField(Author)
    year = db.IntField()

from application import Author, Book
mark_pilgrim = Author(name='Mark Pilgrim')
dive = Book(title='Dive Into Python', author=mark_pilgrim, year=2004)

mark_pilgrim.name = 'Mark Stalone'
mark_pilgrim.save()
Flask-MongoAlchemy example

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…

paginate(page, per_page=20, error_out=True)

Next post I will share some design ideas.

See you tomorrow!

Write a Comment

Comment