Home » Tutorials » How to Build Your First Web Application with Flask in PyCharm

How to Build Your First Web Application with Flask in PyCharm

Do any of you wonder what Flask is? Well, it’s a web framework for Python that allows us to build web applications rapidly and easily. As such in today’s article we’re going to use PyCharm to create a Flask project, set up a virtual environment, and create a functioning basic Flask script.

Let’s get started!

Table of Contents

How to Download PyCharm

  1. First, visit this site: https://www.jetbrains.com/pycharm/download.
  2. Second, decide which version you wish to download: Professional or Community. I use the Community edition because it is free.
  3. Finally, after downloading, simply follow the installation steps until completion.

Setting up a Virtual Environment

A virtual environment is basically a space where you put or in other words install the Python packages that you need to run your scripts. 

Well, using PyCharm when you create a new project you end up creating a default virtual environment with it as seen here:

  • You just need to name your project (I named mine ‘FlaskProject‘) and choose the directory in the ‘Location‘ section.
  • Then, ensure that your ‘venv’ (virtual environment) directory is within your Flask project file.
  • You have the option to make your ‘venv’ a default by selecting ‘inherit global site-packages‘, and making them available to all projects as I did. or you can choose not to tick them and make your ‘venv’ a custom one.

Installing Flask

Once you have set up your virtual environment, what you need now is to install Flask packages by entering this command pip install flask in your terminal after you click Alt+f12.

and then press ‘Enter‘ to get this of course in my case I already installed Flask which is why it says ‘Requirement already satisfied‘.

Naturally, you can also enter the command in your command prompt after going to the directory using cd command:

Writing our First Flask Application

Full Code

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
   return "Hello! Welcome to our personalized greeting app. Please add your name to the URL like this: /greet/<your_name>"

@app.route('/greet/<name>')
def greet(name):
   return f"Hello, {name}! Congratulations on taking your first step in Learning flask."

if __name__ == '__main__':
   app.run(host='0.0.0.0', port=5000, debug=True)

Explanation

Just like any other code, we start with imports, in this case, we start by importing the Flask class from the flask module, in other words by importing the Flask class, we will import the standard blueprint that all web developers with Flask use.

from flask import Flask

Now that we imported the class we need to customize it to suit our objective, for that, we will create an instance with the name app and add to it the variable name to make sure that Flask knows the name of our instance so it can locate anything associated with it.

app = Flask(__name__)

After we have defined our custom class we need to customize it, so here we will create routes or in other words paths (URLs), so when the user types a specific URL Flask will call the appropriate function.

@app.route('/')
def index():
   return "Hello! Welcome to our personalized greeting app. Please add your name to the URL like this: /greet/<your_name>"

@app.route('/greet/<name>')
def greet(name):
   return f"Hello, {name}! Congratulations on taking your first step in Learning flask."

Finally, all that’s left is running the application but before that, we need to make sure that this script can only be run directly.

if __name__ == '__main__':

and also ensure that our Flask application can be accessed from other devices on the same network.

app.run(host='0.0.0.0',

While also making an ear which is port 5000 for our Flask App that listens to the request of the user (URLs), so that our app can call the appropriate route function to fulfill the request of the user and take him to his desired web page.

 port=5000,

and lastly to enhance the development experience and provide helpful debugging features during development.

 debug=True)

Running the Code

Once you run the code you get two URLs:

  • http://127.0.0.1:5000
  • http://192.168.1.7:5000

When you click on either of them Flask calls the first function and you get this:

Which asks you to ‘add /greet/your_name‘ to any of the URLs, and once you do that Flask calls the second so you get this:

Begin Your Journey with Flask

Congratulations on taking your first step to learning Flask! We will be posting more tutorials and real-world applications on our blog, so stay tuned!

Happy Coding!

Subscribe for Top Free Python Tutorials!

Receive the best directly.  Elevate Your Coding Journey!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
×