Christian Lutz
Posted on April 7, 2019
Creating web apps is possibly one of the coolest things you could do in your lifetime. ;)
The reasons are obvious.
You can create something meaningful, everyone can use.
With some work, you may be creating something like this:
Follow me to get more information about the ServerMonitor in the near future.
Let's get started!
Introduction
Flask is a lightweight Python web framework based on Werkzeug and Jinja2.
Lightweight means, Flask only has the most basic features to create your web application on your own.
Everything other like admin panels, for example, have to be built from scratch, unlike in Django.
For starting out in web development with Python, this makes a lot of sense.
You don't have to learn a lot of framework stuff before you can build a web app.
Create your project
Make a folder where your Flask project should be located.
You can do this in CMD on Windows.
mkdir flask_project
Then, open your created folder with:
cd flask_project
Installation
Installing flask via PIP is quite easy.
Simply type...
pip install Flask
...to install the Flask module.
Setting up the application
After installing Flask, we have to make a Python file.
I will call it 'main.py'
This file will contain all of the basic code for Flask to run.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_www():
return "Hello World Wide Web!"
Code explanation
First of all, we import our installed Flask module from Flask with:
from flask import Flask
Then, we define the variable 'app' and assign "Flask(_ name _)" to it:
app = Flask(__name__)
After that, we can create our first route with:
@app.route("/")
To make it work, we need some return value, that will be displayed on our web page.
For that, we type:
def hello_www():
return "Hello World Wide Web!"
Last but not least, we will return a String with "Hello World Wide Web!".
Run your application
After we have created and saved our code, we want to see the result.
For that, you first need to define your Python file for Flask in this way:
set FLASK_APP=main.py
If this is done, you can start your web app with:
flask run
Go to http://localhost:5000 or http://127.0.0.1:5000, to see your result:
Congratulations!
You have created your first web app.
If you want to dive deeper into the Flask-World, visit http://flask.pocoo.org/.
Thank you for reading!
Posted on April 7, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.