How to Run a Flask App
Flask is a lightweight and flexible Python web framework that allows developers to build web applications quickly and efficiently. With its simplicity and ease of use, Flask has become a popular choice for building web applications, especially for small to medium-sized projects. In this article, we will guide you through the process of setting up and running a Flask app.
Step 1: Install Flask
Before you can start building your Flask app, you need to install Flask. You can install Flask using pip, the Python package manager. Here’s how to install Flask:
- Open your terminal or command prompt.
- Type the following command:
pip install flask - Press Enter to run the command.
Step 2: Create a New Flask App
Once Flask is installed, you can create a new Flask app. Here’s how to create a new app:
- Open your terminal or command prompt.
- Type the following command:
flask new myapp - Press Enter to run the command.
- This will create a new directory called
myappwith a basic Flask app structure.
Step 3: Configure the App
Before you can start building your app, you need to configure it. Here’s how to configure the app:
- Open the
app.pyfile in yourmyappdirectory. - Add the following code to the file:
from flask import Flask
app = Flask(name)
@app.route("/")
def index():
return "Hello, World!"
if name == "main":
app.run(debug=True)
* This code creates a new Flask app with a single route for the root URL (`"/"`) that returns the string "Hello, World!".
* The `if __name__ == "__main__":` block is used to run the app in debug mode.
**Step 4: Run the App**
Now that you have configured your app, you can run it. Here's how to run the app:
* Open your terminal or command prompt.
* Type the following command: `python app.py`
* Press Enter to run the command.
**Step 5: Test the App**
Once the app is running, you can test it. Here's how to test the app:
* Open a web browser and navigate to `http://localhost:5000`.
* You should see the string "Hello, World!" displayed on the page.
**Step 6: Add Routes and Views**
Now that you have a basic app up and running, you can add routes and views to create more complex applications. Here's how to add a new route and view:
* Open the `app.py` file in your `myapp` directory.
* Add the following code to the file:
```python
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/about")
def about():
return "This is the about page"
if __name__ == "__main__":
app.run(debug=True)
- This code creates a new route for the root URL (
"/") that renders an HTML template calledindex.html. - The
aboutroute is a simple route that returns the string "This is the about page".
Step 7: Add Models and Database
Now that you have routes and views, you need to add models and a database to store data. Here’s how to add a model and database:
- Open the
models.pyfile in yourmyappdirectory. - Add the following code to the file:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(100), nullable=False, unique=True)
def __repr__(self):
return f"User('{self.name}', '{self.email}')"
* This code creates a new model called `User` with columns for `id`, `name`, and `email`.
* The `db` object is used to interact with the database.
**Step 8: Add a Database**
Now that you have a model and database, you need to add a database to store data. Here's how to add a database:
* Open the `config.py` file in your `myapp` directory.
* Add the following code to the file:
```python
SQLALCHEMY_DATABASE_URI = "sqlite:///myapp.db"
SQLALCHEMY_TRACK_MODIFICATIONS = False
- This code sets the database URI to a SQLite database file called
myapp.db.
Step 9: Run the App Again
Now that you have added a database, you need to run the app again. Here’s how to run the app again:
- Open your terminal or command prompt.
- Type the following command:
python app.py - Press Enter to run the command.
Conclusion
In this article, we have covered the basic steps to run a Flask app. We have created a new app, configured it, run it, and tested it. We have also added routes and views, models, and a database to create a more complex application. With Flask, you can build web applications quickly and efficiently, and with these steps, you can get started with building your own web applications.
Table of Contents
- Step 1: Install Flask
- Step 2: Create a New Flask App
- Step 3: Configure the App
- Step 4: Run the App
- Step 5: Test the App
- Step 6: Add Routes and Views
- Step 7: Add Models and Database
- Step 8: Run the App Again
- Conclusion
