How to Create New Project in Pycharm in Flask


Step 1 : Open PyCharm


Pycharm

Open PyCharm on your computer and make sure you have a compatible Python interpreter installed.

Step 2 : Create a new PyCharm project


New Project

Go to "File" > "New Project" in the top menu of PyCharm to create a new project. Choose a location on your computer where you want to create the project and click "Create".

Step 3 : Choose Python interpreter


Create Project

In the "New Project" dialog box, choose a Python interpreter for your project. If you don't have a Python interpreter configured in PyCharm, you can click "New.." to create a new virtual environment or choose an existing interpreter.

Step 4 : Configure a Python interpreter


Python Interpreter

In PyCharm, go to "File" > "Settings" > "Project: [your_project_name]" > "Python Interpreter". This will open the settings for the Python interpreter used in your project.

Step 5 : Install Flask in the Python interpreter


Flask Install

Once you have selected or created a Python interpreter, you can install Flask in that interpreter. In the "Add Python Interpreter" window, make sure the "Package" tab is selected, and then search for "flask" in the search bar. You can then select "Flask" from the list of packages and click on the "Install Package" button to install Flask in the selected Python interpreter.

Step 6 : Verify Flask installation


Flask Apply

After the installation is complete, you can click on the "OK" button to close the "Add Python Interpreter" window. You can then verify that Flask has been installed in the selected Python interpreter by importing Flask in your Python code.

Step 7 : Write Sample Code

from flask import Flask
 
app = Flask(__name__)
 
@app.route('/')
def index():
	return '<h1>Tutor joes</h1>'
 
if __name__ == '__main__':
	app.run(debug=True)

"from flask import Flask": This line imports the Flask module and makes the Flask functionalities available for use in your code. "app = Flask(__name__)": This line creates an instance of the Flask application class called app. The __name__ argument is a special variable in Python that represents the name of the current module. It is used by Flask to determine the root path of the application.

"@app.route('/')": This line defines a route for the root URL "("/")" of the Flask application. The "@app.route()" decorator is a Flask decorator that associates a URL route with a Python function. In this case, it associates the root URL ("/") with the "index()" function defined below it.

"def index():": This is a Python function called index() that defines the logic for handling requests to the root URL ("/") of the Flask application. In this example, it returns a simple HTML response of "<h1>Tutor joes</h1>". "if __name__ == '__main__':": This conditional statement checks if the current script is being run directly (as opposed to being imported as a module). If it is being run directly, the following block of code will be executed.

"app.run(debug=True)": This line starts the Flask development server and runs the Flask application. The debug=True argument enables the Flask application to run in debug mode, which provides additional information for debugging during development.

Step 8 : Run the Flask App


Run PyCharm

Open a web browser and go to http://127.0.0.1:5000/ to access your Flask app locally.


output

You should see "Tutor joes" displayed in the browser, which means your Flask app is running successfully.