Getting Started with Python: Flask + Gunicorn Hello World Tutorial

Posted by


In this tutorial, we will be going through the process of creating a simple Hello World web application using Flask and Gunicorn in Python. Flask is a lightweight web framework that makes it easy to create web applications in Python, while Gunicorn is a WSGI HTTP server that can be used to serve Flask applications.

Before starting the tutorial, make sure you have Python installed on your system. You can download Python from the official website: https://www.python.org/downloads/. Flask and Gunicorn can be installed using pip, the Python package manager.

Step 1: Setting up the virtual environment
It is always a good practice to create a virtual environment for your Python projects to avoid conflicts with system-wide packages. To create a virtual environment, you can use the following commands in your terminal:

$ python3 -m venv myenv
$ source myenv/bin/activate

Step 2: Installing Flask and Gunicorn
Once you have activated the virtual environment, you can install Flask and Gunicorn using pip:

$ pip install Flask gunicorn

Step 3: Creating the Flask application
Now, let’s create a simple Flask application that will display "Hello World" when accessed. Create a new Python file named app.py and add the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

Save the file and you have created a simple Flask application that defines a route for the root URL (/) and returns "Hello World" when accessed.

Step 4: Running the Flask application with Gunicorn
To run the Flask application using Gunicorn, you can use the following command in your terminal:

$ gunicorn -b 0.0.0.0:8000 app:app

This command starts the Gunicorn server and binds it to the IP address 0.0.0.0 on port 8000, serving the Flask application defined in the app variable in the app.py file.

Step 5: Accessing the Hello World web application
Once the Gunicorn server is running, you can access the Hello World web application by opening a web browser and navigating to http://localhost:8000. You should see the text "Hello World" displayed on the page, indicating that the Flask application is running successfully.

Congratulations! You have successfully created a simple Hello World web application using Flask and Gunicorn in Python. You can now further customize your Flask application by adding more routes and functionality as needed. Happy coding!

0 0 votes
Article Rating
34 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@erikjones1266
2 months ago

Super simple but a great intro! You have great potential as a youtuber!

@optimistcarrot6531
2 months ago

Annoying bug: gunicorn conflicts with the argparse module, creating `gunicorn: error: unrecognized arguments: ` error if not properly handled (for instance using if _name_ =="__main__": on the nested module)

@avinashmangipudi4203
2 months ago

very brief and to the point..thanks 🙂

@cheenamaejafar
2 months ago

Thanks for this straight forward tutorial on gunicorn…batman..

@kia4now
2 months ago

Hey Andrew, I was wondering how do you go and tweak gunicorn settings later on

@rashidulislam9636
2 months ago

Can I post to gunicorn like I did with flask? Apparently its not working for me.

@ColdRooster
2 months ago

Thanks buddy

@mshirazbaig6055
2 months ago

I wish you had spoken more clearly. And also you had made your font more readable. In the absence of these two things, difficult to comprehend, because I do not easily understand your accent

@essamazzam5737
2 months ago

straight to the point,, not waisting a second in the video, thanks

@manfrombritain6816
2 months ago

nice dr house style croaky voice lol

@ochbolton2000
2 months ago

How do I get this to work on windows 10?

@sogood007ha
2 months ago

I use the node app forever to run my python flask app 24/7

@CodeTerra
2 months ago

By default my gunicorn taking the python version 3.8 but I want to use python 3.6
How can I change the python version for gunicorn

@ykartd
2 months ago

Unicorn 😂🤭

@anikethdeshpande8336
2 months ago

very nice explanation.!!!!

@davehouser1
2 months ago

The requirements.txt file is not in the branch you shared.
I looked at your .gitignore, "*.txt" is listed, is this expected? Does not seem like its needed for this video.
Here is a list of everything at 0:56.
flask

gunicorn

Jinja2>=2.10.1

Werkzeug>=0.15

itsdangerous>=0.24

click>=5.1

setuptools>=3.0

MarkupSafe>=0.23

@hritikdj
2 months ago

Thanks andrew 🙂

@ROHITKUMAR-wq2db
2 months ago

Hello Sir i am using flask application and i want my server run on Gunicorn but when i doing all steps according to your video ..and run gunicorn it gives me error ModuleNotFoundError: No module named 'fcntl' and i am using window 10 os ..so i just wanted to know is gunicorn run on window os or not? second how i am run gunicorn in window os

@sukirtiarya1176
2 months ago

How do you pass arg while running the app via gunicorn

@EcControler
2 months ago

Batman is that you?