DEV Community

DoriDoro
DoriDoro

Posted on

1

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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay