Learn Flask - Basics
Flask is a well known Python web framework. Perhaps you know hivehealth wich work with on it, this website allow you to get a report on your health on hive (created by @oniemaniego). @tomoyan also use it in his series of experiments with the python library beem and Bootstrap.
Let's go !
For this first episode, our code will only be in one file. Later in this serie, we will organize our codebase in a more modular way.
Let's create our python file:
touch flask_app.py
Then open it in your favorite editor, and open flask_app.py
.
Let's make sure flask is there (type this in terminal):
pip3 install flask
(type this in terminal)
First we will need to import flask and create a instance of the Flask
class
In flask_app.py
from flask import Flask
app = Flask(__name__)
if __name__ == "__main__":
app.run()
Then we will add the first endpoint of our flask app with the app.route
decorator
in flask_app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'hello world ! '
if __name__ == "__main__":
app.run()
Run flask_app.py with this
export FLASK_ENV=development
export FLASK_APP=flask_app
flask run
Then visit http://127.0.0.1:5000/ on your browser !
You made it to the end, bravo! If you have any questions, don't forget to ask. See you next time! If you spot a grammar or vocabulary mistake, please let me know in the comments.
You will be able to find a good part of the articles in this account in this repository.
To go directly to the section you are currently viewing click here.
@oniemaniego thanks for the repost !
Thanks for the mention. I didn't know @oniemaniego made a report :)
It's quite cool actually.
hey, by the way, oniemaniego and I just created a community : DevTalk, it's for everything about software developpement, feel free to take a look !