Building a Python Flask application — Tutorial 02
Hi all, welcome to the second tutorial of “Building a Python Flask application” tutorial series. In this tutorial, I will explain the basic code structure of the minimal flask application.
The minimal Flask application looks like this:
from flask import Flask
app = Flask(__name__)@app.route('/')
def hello_world():
return 'Hello, World!'
Let’s learn how to write the code step by step.
Step 1: Import Flask class to your application.
from flask import Flask
Step 2: Create an instance of this class.
app = Flask(__name__)
Step 3: Write the URL which you need to run your Flask application inside of the route() decorator.
@app.route(‘/’)
Step 4: Create a function and return the message that you want to display on your browser.
def hello_world():
return 'Hello, World!'
Well done !!! Now you know the basic code structure of the minimal Flask application.
Next, you are going to learn about how to enable the development feature of your application.
When you change something in your code,
- if you have not enabled development features, you should restart the server manually.
- If you have enabled the development features (including debugging) the server will be refreshed (restart) automatically without our concern.
Let’s start
Step 1: Execute following commands to run the flask application
$ export FLASK_ENV=development
$ flask run
Step 2: Change the code as below and save
Step 3: Refresh the browser
Well, now you know how to run the flask application without restarting the server manually.
In the next tutorial, Teach you about the routing of a flask application.
Good Luck!
Bye.
For the first tutorial:
https://medium.com/@dhsashini/building-a-python-flask-application-f051fffa0bfa