Introduction

Flask is one of the most popular Python web frameworks, known for its simplicity and flexibility. Whether you’re a beginner or an experienced developer, Flask makes web development straightforward.

In this tutorial, you’ll learn how to:

  • Set up a Python environment for Flask.
  • Install Flask and create your first web route.
  • Run your app locally.
  • Deploy your Flask web app to the cloud.

By the end of this guide, you’ll have a fully functional web application running live online. 🚀

Want to skip the setup and jump straight to the code? Click here .

Step-by-Step Guide

Before you begin, choose one of the following deployment options, both equally visible:

  1. Deploy using the dashboard – Start with a template created by our team. Deploy the template .
  2. Deploy using your CLI – Follow the step-by-step guide below for manual deployment.

Before you begin, make sure you have Python and pip installed on your machine.

Step 1: Install Flask and Required Libraries

First, let’s set up a virtual environment and install the necessary dependencies:

python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip3 install flask
pip3 freeze > requirements.txt

Explanation:

venv creates an isolated Python environment for the project. Flask is the web framework. requirements.txt ensures consistent dependencies when deploying the app.

Step 2: Create Your Flask Application

Create a file named app.py and add the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, World!"

if __name__ == '__main__':
    app.run(debug=True)

Explanation:

  • Flask initializes the web application.
  • @app.route('/') defines the route for the homepage.
  • app.run(debug=True) starts the development server with debugging enabled.

Step 3: Run Your Flask Application

Run the app locally:

python app.py

Open a browser and navigate to http://127.0.0.1:5000.

You should see “Hello, World!” displayed.

Deployment Guide

Step 1: Install genezio CLI

Run the following command:

npm install -g genezio

Step 2: Create the configuration file

Run the following command:

genezio analyze

Explanation:

  • Genezio scans your project to create a configuration file, genezio.yaml.
  • During this process, you’ll be prompted to enter the project name and region in the terminal.

Step 3: Test Locally

Run your app locally using Genezio:

genezio local

This command runs your app in a local development environment.

Step 4: Deploy your app with Genezio

Finally, deploy your app to the cloud with a single command:

genezio deploy

Your app will be live at a custom subdomain, such as https://your-app-name.app.genez.io.

You can continue to manage, test, update and monitor your project from the genezio dashboard.

Conclusion

You’ve successfully built and deployed your first Flask web app in just 10 minutes.

Join Our Community!

Need help or want to connect with fellow developers? Join us on the Genezio Discord server .

For questions, feel free to contact me at cristi@genezio.com.

The code for this tutorial is open-source and available on GitHub .

Happy coding! 🚀

Subscribe to our newsletter

Genezio is a serverless platform for building full-stack web and mobile applications in a scalable and cost-efficient way.



Related articles


More from Tutorials