This content originally appeared on DEV Community and was authored by Siddhartha Roy
A complete Flask tutorial for beginners
I made this tutorial to help and teach my students make awesome dynamic websites using Flask. Flask is an API of Python that allows us to build up web-applications. It was developed by Armin Ronacher. It's Modern and very expressive, I learned it because it's just super useful and also allows me to write less code.
I tried to remove the extra topics of Flask and made this as beginner friendly as possible. So let's get started with Installation and then get an overview with the Quickstart. This Tutorial will show how to create a small but complete application with Flask.
Best of luck to you!
Table of Content
Required:
- A little experience with coding in python (variables, loops, methods/functions, etc)
- Patience
- Time
Note this is a tutorial for Backend Development, not Frontend Development. Large software companies like Google, Amazon,
Facebook and Microsoft view Backend Developers as different from Frontend Developers. Yet, in order to become a good programmer one need to understand the concepts of both.
Docs
Fask Docs
Jinja Docs
Why to choose Flask
Flask's framework is more explicit than Django's framework and is also easier to learn because it has less base code to implement a simple web-Application.
List of companies using Flask framework - who is using Flask?
Companies using Flask
- Red Hat , Rackspace, Airbnb, Netflix, PythonAnywhere, Lyft, Reddit, Mailgun, MIT, Mozilla, Balrog (Application Update Service), Release Engineering Services, Hotjar, Patreon, Teradata, Uber, Samsung, Nginx, +1.5k more companies in https://stackshare.io/flask/
Quickstart
Installation
pip install flask
Minimal app
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
if __name__=="__main__":
app.run()
So what did that code do?
First we imported the Flask class. An instance of this class will be our WSGI application.
Next we create an instance of this class. The first argument is the name of the application’s module or package. name is a convenient shortcut for this that is appropriate for most cases. This is needed so that Flask knows where to look for resources such as templates and static files.
We then use the route() decorator to tell Flask what URL should trigger our function.
The function returns the message we want to display in the user’s browser. The default content type is HTML, so HTML in the string will be rendered by the browser.
Debug Mode
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
# debud mode running on 8000 port
if __name__=="__main__":
app.run(debug=True, port=8000)
The flask run command can do more than just start the development server. By enabling debug mode, the server will automatically reload if code changes, and will show an interactive debugger in the browser if an error occurs during a request.
Warning ⚠️
The debugger allows executing arbitrary Python code from the browser. It is protected by a pin, but still represents a major security risk. Do not run the development server or debugger in a production environment.
Routing
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'This is Index Page'
@app.route('/login')
def login():
return 'This is Login Page'
@app.route('/hello')
def hello():
return 'Hello, World'
if __name__=="__main__":
app.run(debug=True)
Modern web applications use meaningful URLs to help users. Users are more likely to like a page and come back if the page uses a meaningful URL they can remember and use to directly visit a page.
Use the
route()
decorator to bind a function to a URL.
Url Variables
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
# string
@app.route('/string/<string:value>')
def string(value):
return f"<p>Hi this is a string value {value}</p>"
# int
@app.route('/int/<int:value>')
def int(value):
return f"<p>Hi this is a int value {value}</p>"
# float
@app.route('/float/<float:value>')
def float(value):
return f"<p>Hi this is a float value {value}</p>"
# path
@app.route('/path/<path:value>')
def path(value):
return f"<p>Hi this is a path value {value}</p>"
# uuid
@app.route('/uuid/<uuid:value>')
def uuid(value):
return f"<p>Hi this is a uuid value {value}</p>"
if __name__=="__main__":
app.run(debug=True)
You can add variable sections to a URL by marking sections with
<variable_name>
. Your function then receives the<variable_name>
as a keyword argument. Optionally, you can use a converter to specify the type of the argument like converter:variable_name.
Type | Value | Use |
---|---|---|
string | (default) accepts any text without a slash | string:value |
int | accepts positive integers | int:value |
float | accepts positive floating point values | float:value |
path | like string but also accepts slashes | path:value |
uuid | accepts UUID strings | uuid:value |
This content originally appeared on DEV Community and was authored by Siddhartha Roy
Siddhartha Roy | Sciencx (2021-09-03T20:57:25+00:00) A complete beginner friendly Python Flask tutorial ?. Learn from basic template rendering to deploying in web servers.. Retrieved from https://www.scien.cx/2021/09/03/a-complete-beginner-friendly-python-flask-tutorial-%f0%9f%90%8d-learn-from-basic-template-rendering-to-deploying-in-web-servers/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.