Logo

dev-resources.site

for different kinds of informations.

How to create a simple Flask application

Published at
12/3/2024
Categories
flask
webdev
Author
doridoro
Categories
2 categories in total
flask
open
webdev
open
Author
8 person written this
doridoro
open
How to create a simple Flask application

Introduction:

For this article, I wanted to create a guide to building a simple Flask app from scratch. This guide is just to get you through the first steps of creating a Flask application.

creating a simple Flask app

My favourite IDE is PyCharm. To create a simple Flask application, I have created a new project within PyCharm. The advantage of doing that in PyCharm is, that my IDE creates immediately a virtual environment.

You have to install Flask in your virtual environment:

pip install Flask
Enter fullscreen mode Exit fullscreen mode

Save the dependencies in a file (requirements.txt) in root directory with command:

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Legend:

  • pip freeze: This command creates a list of all your installed packages in the virtual environment, along with their versions.
  • > requirements.txt: This part of the command saves all the installed packages and their versions into a file called: requirements.txt. When this file doesn't exist that command creates this file in root directory.

To create a simple Flask application, you have to create in your root directory a file named: 'server.py' (you can name it whatever you like but AVOID to name it 'flask.py') and add this:

# server.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"
Enter fullscreen mode Exit fullscreen mode

Legend:

  1. We import the Flask class at the beginning of our file. An instance of this class will be our WSGI application.
  2. Next we create an instance of the Flask class.
  3. The route() decorator tells Flask which URL triggers our function.
  4. In this simple example, the function returns a paragraph with the content of "Hello World!".

The next step is to start the server:

flask --app server run --debug
Enter fullscreen mode Exit fullscreen mode

Legend:

  • flask: Is the Flask CLI tool to manage the application.
  • --app server: This part specifies that the Flask application is defined in the server.py file.
  • run: This command starts the Flask development server and serves the application locally.
  • --debug: This part enables the debug mode of the Flask application.

And that is it, you can go to your browser and enter: http://127.0.0.1:5000 and you should see "Hello World!" displayed on your browser screen.

Flask documentation

flask Article's
30 articles in total
Favicon
Deploy your Flask API on GCP Cloud Run πŸš€
Favicon
RESTful GET and POST Requests: A Beginners Guide
Favicon
Flask Routes vs Flask-RESTful Routes
Favicon
Bringing Together Containers & SQL
Favicon
Creating a Local Environment to Operate GCS Emulator from Flask
Favicon
Optimising Flask Dockerfiles: Best Practices for DevOps and Developers
Favicon
A beginners guide to Constraints and Validations in Flask, SQLAlchemy
Favicon
Deploying Flask-based Microservices on AWS with ECS Service Connect
Favicon
FastAPI + Uvicorn = Blazing Speed: The Tech Behind the Hype
Favicon
CRUD With Flask And MySql #2 Prepare
Favicon
CRUD With Flask And MySql #1 Introduction
Favicon
Building an Anemia Detection System Using Machine Learning πŸš‘
Favicon
Como usar WebSockets em Flask (How to use WebSockets in Flask)
Favicon
Setup Celery Worker with Supervisord on elastic beanstalk via .ebextensions
Favicon
How to create a simple Flask application
Favicon
Flask
Favicon
Building and Testing the Gemini API with CI/CD Pipeline
Favicon
Crossing the Line before the Finish Line. Also the line before that.
Favicon
Mastering Python Async IO with FastAPI
Favicon
Webinar Sobre Python e InteligΓͺncia Artificial Gratuito da Ebac
Favicon
Is Flask Dead? Is FastAPI the Future?
Favicon
422 Error with @jwt_required() in Flask App Deployed on VPS with Nginx
Favicon
WSGI vs ASGI: The Crucial Decision Shaping Your Web App’s Future in 2025
Favicon
Building a Real-Time Flask and Next.js Application with Redis, Socket.IO, and Docker Compose
Favicon
Carla Simulator 2 : Welcome to the Ride πŸš—πŸοΈ
Favicon
Python: A Comprehensive Overview in One Article
Favicon
Understanding Authentication: Session-Based vs. Token-Based (and Beyond!)
Favicon
Building RESTful APIs with Flask
Favicon
Validatorian
Favicon
LumaFlow

Featured ones: