Flask is a lightweight web framework for Python, known for its simplicity, flexibility, and fine-grained control. Unlike more ‘heavyweight’ frameworks like Django, Flask does not come with many built-in tools or components, which means that developers have the freedom to choose the most suitable tools for their projects. In this article, we’ll go through the basics of setting up a Flask web application.
Pre-requisites
Before you get started with Flask, you’ll need to have Python installed on your machine. Flask supports Python 3.6 and newer versions. You’ll also need pip, the Python package installer.
Step 1: Setting Up Your Environment
Firstly, it’s best practice to set up a virtual environment when developing Python applications. This ensures your project’s dependencies are isolated from those of other projects. Here’s how to do it:
mkdir flask-app && cd flask-app
python3 -m venv myenv
This creates a new virtual environment named “myenv”. To start using this environment, you’ll need to activate it like so:
On Unix or MacOS:
source myenv/bin/activate
On Windows:
.\myenv\Scripts\activate
Once activated, your terminal prompt should change to show the name of your activated environment.
Step 2: Installing Flask
With your virtual environment activated, you can now install Flask using pip:
pip install Flask
Step 3: Your First Flask Application
Now, let’s create a basic Flask application. Open a new file named “app.py” and add the following:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, Flask!"
if __name__ == '__main__':
app.run(debug=True)
Here’s what this code does:
- The from flask import Flask line imports the Flask module.
- app = Flask(__name__) creates a new Flask web server from the Flask module.
- @app.route(‘/’) is a Python decorator that Flask provides to assign URLs in our app to functions. This decorator tells Flask to call the home() function whenever someone visits our app’s root URL.
- def home(): return “Hello, Flask!” defines a function that returns the string “Hello, Flask!”, which will be displayed in the user’s browser when they visit our app’s root URL.
- if __name__ == ‘__main__’: app.run(debug=True) is a Python idiom for a script to be executed when it is run directly. The app.run(debug=True) runs the application on the local development server and the debug=True argument enables debug mode, providing detailed error messages in the browser if something goes wrong.
Step 4: Running Your Flask Application
Now, you can run your new Flask application with the following command:
python app.py
You should see output telling you that the server is running. Open a web browser and navigate to http://localhost:5000 to see your application in action.
Conclusion
This is just the tip of the iceberg of what’s possible with Flask. As a micro web framework, Flask is designed to be extended with plugins to add any functionality you need. For example, Flask doesn’t provide a way to handle forms or databases out of the box, but you can easily add this functionality using Flask extensions like Flask-WTF for forms and Flask-SQLAlchemy for databases.
The Flask documentation is an excellent resource for getting to grips with this flexible framework. As always, the key to learning is doing, so the best way to learn Flask is to start building. Happy coding!