Github,  Python

Python and Flask

I have been learning flask for about 1 year now.

It has been fun, frustrating at times and I wish I would have finished the full documentation for it. But at least now I know how the different parts fit together, and all the “magic” that is inculde in Django is a developer’s choice when using flask.

But what a great micro web framework to use!

Flask is super easy to setup and works great if you are using RAD (Rapid-application development) or have a medium to small size app, it might work really good for bigs apps also, but then there is a lot of choices to consider for extensions and libraries to use.

  • Pip install flask in your env
  • Make a flask app
  • Make a function with a route and then head to localhost:port/route

from flask import Flask, render_template

app = Flask(__name__)

@app.route(“/”)

def index()

#return txt

return “hello world”

#return html

return render_template(“index.html”) index location app/templates/index.html

 

Good tips for bigger apps, use blueprints in flask.

But now I wanna try and learn Django for e-lo.

Short about flask

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself. Extensions exist for object-relational mappers, form validation, upload handling, various open authentication technologies and several common framework related tools.

testit.tech with flask:

Bootstrap menu:

Login manger:

Note admin, with get all, get topic, new note, edit note and delete note:

Note get topic:

Note add, new note:

Note edit:

Pygal library testing:

pip freeze > requirements.txt

alembic==0.9.10
click==6.7
dominate==2.3.1
Flask==0.12.2
Flask-Bootstrap==3.3.7.1
Flask-Login==0.4.1
Flask-Migrate==2.2.1
Flask-SQLAlchemy==2.3.2
Flask-WTF==0.14.2
future==0.16.0
futures==3.2.0
gunicorn==19.7.1
itsdangerous==0.24
Jinja2==2.9.6
Mako==1.0.7
MarkupSafe==1.0
pygal==2.4.0
python-dateutil==2.7.3
python-editor==1.0.3
six==1.11.0
SQLAlchemy==1.2.9
visitor==0.1.3
Werkzeug==0.12.2
WTForms==2.1

testit.tech was developed with:

  • Flask, Python micro webframwork
  • Jinja2 for templating, html
  • Bootstrap
  • Gunicorn webserver
  • Nginx, reverse proxy
  • Digital Ocean for hosting with linux
  • A blog and a note-app was developed

 

Source code for testit.tech:

https://github.com/spawnmarvel/testit.tech2

 

 

The text below is not part of tutorial, for internal reference only.

Install Nginx, remove default site, create your site, load gunicorn:

nginx pdf

Good to know:

Envs:

source/bin/activate

deactivate

Gunicorn:

wsgi.py:

from app import app

if __name__==”__main__”:

app.run()

(env) gunicorn –bind 0.0.0.0:port wsgi:app

Templates / static not loading after SFTP, stop / start gunicorn

Linux:

sudo lsof -i:port

sudo lsof -i:8000

kill pid

Not finding pid, pkill gunicorn

More on Gunicorn:

Thread

100

Workers increase, things got messed up

https://stackoverflow.com/questions/35837786/how-to-run-flask-with-gunicorn-in-multithreaded-mode

Hang in guniorn, showing old data from views (even though the code changed), cache issue, reboot server.

To reboot immediately you have to specify either of the cmds:

sudo shutdown -r 0

sudo shutdown -r now

 

 

 

Comments Off on Python and Flask