Skip to content
Jeremie Tamburini edited this page Nov 16, 2021 · 32 revisions

Disclaimer

‼️ If you have in mind to clone my repo and use it in production... just think twice! As a learning project I can't guarantee the correctness of the contents, there could be mistakes and it could be unsafe (not following best practice while building the project).

I'm using this repo just to learn Flask web framework, building a blog step by step following The Flask Mega-Tutorial by Miguel Grinberg.
While studying a technology I like to document the topic I'm learning. This wiki will help me to fix the concepts 😉

Index

  1. Quickstart Flask on Ubuntu
  2. Run and stop Flask
  3. flaskenv
  4. Shell context
  5. Forms
  6. Database
  7. Password

Quickstart Flask on Ubuntu

The Quickstart Flask on Ubuntu shows how to start with a basic but working project.

Module vs Package

As reported in many tutorials there are two ways to deliver a Flask project:

  • module: good for small projects (even a single file project)
  • package: good for bigger project so that you can separate routing, templates, database, users... into separated files and folders.

The "package way" is the chosen one for this microblog project.

Structure

microblog/
| venv/
| templates/
|   | index.html
| app/
|   | __init__.py
|   | routes.py
| microblog.py
| .flaskenv

Run and stop Flask

  1. Run these commands into the shell:
    cd microblog
    source env/bin/activate
    flask run
    
  2. Click on this link http://127.0.0.1:5000/

Sharing the connection

It's possible that you would like to see how the website looks on smartphones. If you are using a trusted local network (remember this project is running in development mode which is unsafe!!), then the --host option can be add to the flask run command:

flask run --host=0.0.0.0

This makes the website on your computer visible in the local network. For instance, if your IP is 192.168.0.5, the website will be reached at this address: http://192.168.0.5:5000/

Stop server and exit venv

  1. To stop the server press [Ctrl + C]
  2. To exit from virtual environment ran the command:
    deactivate
    

flaskenv

  • Every time you start a session you should export the FLASK_APP variable, running export FLASK_APP=microblog.py.
  • Every time you change a file you should stop and restart the server.

Annoying? Yes!

Microblog/.flaskenv file makes things easier:

  1. Install python-dotenv package:
    pip install python-dotenv
    
  2. Create the Microblog/.flaskenv file with the following strings inside:
    FLASK_APP=microblog.py
    FLASK_ENV=development
    
    ‼️ the development option is not meant to be used in production.

Now start the server just typing flask run and after you have changed some files, just refresh the page to see the result!

Shell context

Sometimes it can be useful to run the Python shell in virtual environment typing:

flask shell

... but it can be annoying to load every time the same modules at the beginning of the session.

Fortunately it's possible to configure microblog.py so that you'll find modules ready to use. In this case the file has been configured to load useful stuff related to the database:

from app import app, db
from app.models import User, Post

@app.shell_context_processor
def make_shell_context():
    return {'db': db, 'User': User, 'Post': Post}

Forms

Login form

See the pages:

Database

Pips

pip install flask-sqlalchemy flask-migrate

Database files

See the pages:

Database commands

  • Create database:
    flask db init
    
  • The following commands need to be run after changing models.py.
    flask db migrate -m "type a message"
    flask db upgrade
    
  • In case of problems you can go back running:
    flask db downgrade
    

Managing db elements via Flask shell

  • Create user with username and email:
    u = User(username='sara', email='sara@mail.com')  # create user
    
  • Save (or not) data into db and delete it:
    db.session.add(u)        # add user 'u' to db (only for this session!)
    db.session.rollback()    # cancel changes
    db.session.commit()      # make changes permanent
    db.session.delete(u)     # delete user 'u'
    
    
  • Queries:
    User.query.all()         # Create list with all users
    User.query.get(1)        # return user with ID = 1
    

Password

See page: Password