Build A Simple Web Application With Flashk
Build A Simple Web Application With Flashk
What is Flask?
Flask is a lightweight, easy-to-use micro-framework for building web applications
in Python. It provides essential tools to get a basic web application up and
running.
Setting Up Flask:
1. Install Flask:
```bash
pip install flask
```
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, World!"
if __name__ == "__main__":
app.run(debug=True)
```
@app.route('/hello/<name>')
def hello(name):
return render_template('hello.html', name=name)
```
Connecting to a Database:
- Use SQLite for simplicity.
- Install SQLite:
```bash
pip install flask-sqlalchemy
```
- Example for setting up an SQLite database:
```python
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
```
Deploying to Heroku:
1. Create a `Procfile`:
```
web: python app.py
```
2. Initialize a Git repository and push to GitHub.
3. Deploy using the Heroku CLI.
Conclusion:
Flask is a simple yet powerful framework for building web applications. By
following these steps, you can quickly create a basic web app and deploy it online.