First Flask App

beginner
6 min

First Flask App

Welcome to the exciting world of Flask, a micro web framework written in Python! In this lesson, we'll guide you through creating your first Flask app. šŸŽÆ

What is Flask?

Flask is a lightweight and easy-to-use web framework for Python. It allows you to quickly develop web applications without the complexity of larger frameworks. šŸ“

Prerequisites

Before we dive in, make sure you have:

  • Python installed on your system
  • Basic understanding of Python syntax

Setting Up Your First Flask App

  1. Install Flask using pip:
bash
pip install flask

Creating Your First Flask App

Now let's create a simple Flask app that serves a "Hello, World!" message.

python
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return "Hello, World!" if __name__ == '__main__': app.run(debug=True)

šŸ“ Note: Each Flask app starts with creating a new Flask web server. The __name__ == '__main__' checks if the script is being run directly (not imported as a module) and starts the server.

Running Your Flask App

Save the code above in a file named app.py and run it:

bash
python app.py

Now open your browser and visit http://localhost:5000/. You should see "Hello, World!" displayed. šŸŽ‰

Next Steps

In the next part of this tutorial, we'll explore more about Flask routes, handling user inputs, and connecting to databases. Stay tuned! šŸ’”

Quick Quiz
Question 1 of 1

Which command is used to install Flask?