Flask is a popular web framework for Python that can be used to build web applications.
If you’re running the project on Replit, you can skip the install step, it’s a default install on Replit python repls.
To use Flask, you must first install it. You can install Flask by running the following command in your terminal:
pip install flask
Once Flask is installed, you can follow these steps to create a basic Flask application:
- Import the Flask module and create a Flask web server object:
from flask import Flask
app = Flask(__name__)
- Define a function that will run when a client requests a specific URL using the route() decorator. For example, to create a route for the home page, add the following code to your Python file:
@app.route('/')
def home():
return 'Hello, World!'
- Finally, run the Flask application with the following code:
if __name__ == '__main__':
app.run()
This code snippet creates a web server that listens to incoming requests on port 5000.
You can access your home page (if locally hosted) by navigating to http://localhost:5000/
in your web browser.
If your project is hosted on replit, access it from the URL that looks like one of these:
https://projectname.username.repl.co
https://projectname--username.repl.co
This is just a basic introduction to Flask. Flask provides many more features, such as the ability to render HTML templates and handle form data.
Let’s continue with some more advanced features of Flask.
- To render an HTML template, you can use the
render_template
function from Flask. First, you need to create atemplates
folder in your project directory, and then create an HTML file inside that folder. For example, if you create an HTML file calledindex.html
inside thetemplates
folder, you can render it with the following code:
from flask import render_template
@app.route('/')
def home():
return render_template('index.html')
- To handle form data, you can use the
request
object from Flask. Here’s an example of how to handle the form data from a POST request:
from flask import request # go learn more about requests here: https://ask.replit.com/t/what-you-need-to-know-about-the-requests-module/36625?u=idkwhttph
@app.route('/submit', methods=['POST'])
def submit():
name = request.form['name']
email = request.form['email']
# do something with name and email
- Flask lets you easily add arguments to a url. For example, an argument would be
https://example.com/?argumentName=value
. You can also pass multiple arguments to a url by doinghttps://example.com/?argumentOne=valueOne&argumentTwo=valueTwo
. To retrieve these arguments in your Python code, you may do:
from flask import request
# rest of the code for your app
@app.route('/args')
def args():
arguments=request.args
return [x for x in request.args.values()]
- Flask provides a convenient way to redirect the user to a different URL using the
redirect
function. Here’s an example of how to redirect the user to the home page:
from flask import redirect, url_for
@app.route('/redirect')
def redirect_example():
return redirect(url_for('home'))
- Flask also lets you create app routes on demand. For example:
# rest of the code for your app
@app.route('/<param1>')
def example(param1):
animals={
"cat": {
"name": "Bob"
}
}
return animals[param1]
These are just a few examples of the many features that Flask provides.
Flask has extensive documentation available online, which can be found at the following link: Welcome to Flask — Flask Documentation (2.0.x)
This documentation includes a Quickstart guide for beginners, a detailed User’s Guide, and a set of API reference documents for each module and class in Flask. The documentation also includes examples and tutorials to help you get up to speed with Flask quickly.
In addition to the Flask documentation, you may also find the official Python documentation and the Werkzeug documentation helpful, as Flask is built on top of these libraries.