How to create a Simple Flask app in just 5 minutes | Python Flask Tutorial for beginners

2 min read 8 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

  1. Open Visual Studio Code editor.
  2. Create a new directory for your Flask project. For example, "simple-flask-app".
  3. 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) or source bin/activate (for Mac/Linux).

Step 3: Installing Flask

  1. 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

Step 4: Creating the Flask Application

  1. Create a new Python file named app.py in your project directory.
  2. Inside app.py, import Flask and create a basic Flask application with a route that returns "Hello World" when accessed.
  3. 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

  1. Run the Flask application using the command:
    • python -m flask run or python -m flask --app app.py run
  2. Access the Flask app in your web browser to see the output.
  3. 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!