How to create a Simple Flask app in just 5 minutes | Python Flask Tutorial for beginners
2 min read
7 months ago
Published on Apr 21, 2024
This response is partially generated with the help of AI. It may contain inaccuracies.
Table of Contents
How to Create a Simple Flask App in Just 5 Minutes
Step 1: Introduction to Flask
- Flask is a lightweight Python framework for web applications that provides basics for URL routing and page rendering.
- It is a micro framework that requires additional Python packages called Flask extensions for features like form validation, database abstraction, and authentication.
Step 2: Setting Up the Environment
- Open Visual Studio Code editor.
- Create a new directory for your Flask project. For example, "simple-flask-app".
- Create and activate a virtual environment using the following commands:
python -m venv venv
(to create the virtual environment)- Navigate to the virtual environment directory and activate it using
.\Scripts\activate
(for Windows) orsource bin/activate
(for Mac/Linux).
Step 3: Installing Flask
- Install Flask in the virtual environment by running the following commands:
- Upgrade pip:
python -m pip install --upgrade pip
- Install Flask:
python -m pip install flask
- Upgrade pip:
Step 4: Creating the Flask Application
- Create a new Python file named
app.py
in your project directory. - Inside
app.py
, import Flask and create a basic Flask application with a route that returns "Hello World" when accessed. - Sample code snippet for
app.py
:from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello World from Flask' if __name__ == '__main__': app.run()
Step 5: Running the Flask App
- Run the Flask application using the command:
python -m flask run
orpython -m flask --app app.py run
- Access the Flask app in your web browser to see the output.
- To stop the application, use
Ctrl + C
.
Step 6: Conclusion
- Congratulations! You have successfully set up a simple Flask app using Visual Studio Code in just a few minutes.
- Stay tuned for more advanced Flask tutorials in the future.
Additional Notes:
- If you encounter any issues or have questions, feel free to ask in the comment section of the video.
- Thank you for watching and have a nice day!