Building a Python Flask application — Tutorial 01
Hi all, welcome to the first tutorial of “Building a Python Flask application” tutorial series. Through this tutorial series, you can learn how to build a Python Flask application and how to insert and retrieve data from the MySQL database.
I hope you all have some knowledge about the Python language and the basic idea about the Python Flask web framework.
At the end of this tutorial, you can learn how to install Python Flask and Run it.
I am using Python 3.5, Flask 1.0.2 and Ubuntu 16.04 for implementation.
Let’s see how to install Python Flask application using your terminal.
Step 1: Create a project directory
mkdir myproject
Step 2: Change the current directory to your project directory
cd myproject
Step 3: Create a virtual environment (using Python 3.5)
python3 -m venv venv
Step 4: Activate the virtual environment. Run the following command in your terminal
. venv/bin/activate
Step 5: Install flask
Well Done !!! you have finished the installation.
Let’s make a simple flask application and run it.
Step 1: Open the text editor and type the following code
from flask import Flaskapp = Flask(__name__)@app.route('/')
def hello_world():
return 'Hello World!'
Step 2: Save it as “app.py” inside the myproject directory
OK, now let’s run your first Flask application
$ export FLASK_APP=app.py
$ flask run
Go to this link
http://127.0.0.1:5000/
You can see “Hello World!” as the output
Well done ! You finished the installation and run your first flask application.
Now let’s see how to create a Flask application using PyCharm editor. It ’s very easy…
Step 1: Download PyCharm editor according to your operating system
Step 2: Install it and create a new project.
File > New Project and select Flask project and click the create button
Step 3: You can see the default code appearing in your window
Third Step: Click the run button to run the application
You can see the following window
If you go to the selected URL in the screenshot, you will see following output in your browser
Great!!
Now you know how to install Flask and how to run it using the terminal and PyCharm IDE.
In the next article, I will explain the basic code structure of the minimal flask application.
Good Luck!
Bye.