a guide to flask
Make a Web App With Flask
Section titled “Make a Web App With Flask”What is Flask?
Section titled “What is Flask?”Flask is a lightweight Python web framework. You just write Python functions that return web pages, making backend development way easier.
Before we start… This guide assumes you know basic git and have Python installed on your machine.
- Create an empty directory and open a terminal.
- Create a new file called
requirements.txtand writeflaskinside it. - Run the following in your terminal:
Terminal window pip install -r requirements.txt# If that doesn't work:pip3 install -r requirements.txt - Create a new file called
app.pyand write the following:
Routes
Section titled “Routes”Routes are what you’ll need to display HTML in the browser.
- In your project directory, create a folder called
templates, and inside it create anindex.htmlfile. - Back in
app.py, belowapp = Flask(__name__), add your routes. - Run the app:
Terminal window python3 app.py - Visit
http://127.0.0.1:5000/— you should see a blank white page. - Go to your
index.htmlfile and write the following:<p>Hello World!</p> - Back at
http://127.0.0.1:5000/, you should now see “Hello World!”
Jinja Templating
Section titled “Jinja Templating”Flask lets you pass Python variables into HTML using Jinja Templating.
-
In
app.py, edit theindex()function — create auser_namevariable and pass it when rendering:@app.route("/")def index():user_name = "Mustafa"return render_template("index.html", user_name=user_name) -
In
index.html, edit the<p>tag to display the variable:<p>Hello {{ user_name }}!</p> -
Reload your tab — you should see whatever name you set in
user_name. -
Try it on another route — in
app.py, add a/testingroute:@app.route("/testing")def testing():user_name = "Mustafa"return render_template("index.html", user_name=user_name) -
Visit
http://127.0.0.1:5000/testingto see it in action. Going back to/will still show your original name.
Templates
Section titled “Templates”As your site grows, use a base template to avoid repeating HTML across pages.
-
In the
templatesdirectory, createbase.html:<!DOCTYPE html><html><head><title>My Site</title></head><body>{% block content %}{% endblock %}</body></html> -
Edit
index.htmlto extend it:{% extends "base.html" %}{% block content %}<p>Hello {{ user_name }}!</p>{% endblock %} -
Create
testing.htmlin the same directory:{% extends "base.html" %}{% block content %}<p>Testing page — Hello {{ user_name }}!</p>{% endblock %} -
In
app.py, update the/testingroute to usetesting.html:return render_template("testing.html", user_name=user_name) -
Visit
http://127.0.0.1:5000/testingto see the new template in action.
Adding CSS
Section titled “Adding CSS”-
Create a
staticdirectory in your project root, and inside it createstyles.css. -
Link the stylesheet in
base.html:<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}"> -
Test it out — try changing the background color in
styles.css:body {background-color: #f0f0f0;} -
Reload your page to see the result.
Deploying Your Project
Section titled “Deploying Your Project”You can use free hosting platforms such as Vercel to host your project.
Written by Mustafa