Create new flask app
Here's a step-by-step guide to creating a new Flask app:
Step 1: Install Flask
If you haven't already, install Flask using pip:
pip install flask
Step 2: Create a new directory for your app
Create a new directory for your Flask app, e.g. my_flask_app
:
mkdir my_flask_app
Step 3: Create a new file for your app
Create a new file called app.py
in the my_flask_app
directory:
touch my_flask_app/app.py
Step 4: Write your Flask app code
Open the app.py
file and add the following code:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "Hello, World!"
if __name__ == "__main__":
app.run(debug=True)
This code creates a new Flask app, defines a single route ("/"
) that returns the string "Hello, World!", and runs the app in debug mode.
Step 5: Run your app
Run your app using the following command:
python app.py
This will start the Flask development server, and you can access your app by navigating to http://localhost:5000
in your web browser.
Step 6: Test your app
Open a web browser and navigate to http://localhost:5000
. You should see the string "Hello, World!" displayed on the page.
That's it! You've created a new Flask app and run it successfully.
Additional steps:
- To create a virtual environment for your app, run
python -m venv myenv
and then activate it usingsource myenv/bin/activate
(on Linux/Mac) ormyenv\Scripts\activate
(on Windows). - To install additional dependencies, use
pip install <package_name>
. - To run your app in production mode, use
gunicorn
oruwsgi
instead of the Flask development server.