31
loading...
This website collects cookies to deliver better user experience
This tutorial assumes that you have Python installed in your Mac or PC (required) and that you have done object-oriented programming in Python before. These prerequisites (except the former one) are recommended but not required.
flask --version
.python -m flask --version
or python3 -m flask --version
.# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "Hello, world!"
# Omittable
if __name__ == "__main__":
app.run(debug=True)
from flask import Flask
- seems a bit strange that you have to import Flask, again from flask? What is happening here is that you are importing a class named "Flask" (capitalized) from the actual module that we installed before, named flask.app = Flask(__name__)
- Here, we are instantiating the Flask class and declaring it in a variable named "app"; this app variable is the main structure of our app that can handle routes, run the app, debug it, etc.@app.route('/')
- Now, this is a Python decorator.def index(): return "Hello, world!"
- This line of code defines a function named index and the above decorator installs this function to the app instance of Flask class, then this function returns "Hello, world!" which would be displayed in the user's browser as an HttpResponse.flask --version
commands worked in your terminal window. These lines just run the app, only if the user is actually running the "app.py" file and not when importing the file from somewhere else.flask run
to run it via the terminal window or command prompt (in Windows), or just run the python file to run it directly (python app.py
).Note: Try changing the value of "Hello, world!" in the 8th line to see the result being reflected on the browser.
return "<h1>Hello!</h1>"
), but that isn't the way to do it. The following steps show you how to add an html file to the server and display it to the user to make it an actual web app.<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<h1>Hello, world</h1>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
</body>
</html>
Note: I've added additional Bootstrap CDN to style our web app.
from flask import Flask
to something like:from flask import Flask, render_template
return "Hello, world!"
to something like:return render_template('index.html')
Note: if you are using the command flask run
, you may have to stop the server (Ctr + C) and rerun it to see the changes.
# app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/greet')
def greet():
return render_template('greet.html')
# Omittable
if __name__ == "__main__":
app.run(debug=True)
from flask import Flask, render_template, url_for
<link rel="stylesheet" href="{{url_for('static', filename='style.css')}}">
...
<script src="{{url_for('static', filename='script.js')}}"></script>
{{}}
are Jinja syntaxes. Now, Jinja is a templating language used by Flask to serve dynamic pages. render_template
function, like:return render_template('index.html', name="Brian")
<h1>Hello, {{name}}!</h1>
If you are having any queries or suggestions related to this topic, feel free to mention it in the comments below, and I will try my best to reply for the same!