Jul 10, 2024
virtualenv
package)Install Python
python --version
python3
Set Up Virtual Environment
pip3 install virtualenv
virtualenv venv
source venv/bin/activate
Install Flask Packages
pip3 install Flask
pip3 install Flask-SQLAlchemy
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "Hello, world!"
if __name__ == '__main__':
app.run(debug=True)
python3 app.py
localhost:5000
templates
static
index.html
in templates
from flask import render_template
@app.route('/')
def index():
return render_template('index.html')
base.html
index.html
static/css/main.css
base.html
using Flask's url_for
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
from flask_sqlalchemy import SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
class ToDo(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(200), nullable=False)
date_created = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return f'<Task {self.id}>'
python3
from app import db
db.create_all()
index.html
<form action="{{ url_for('index') }}" method="POST">
<input type="text" name="content" id="content">
<input type="submit" value="Add Task">
</form>
from flask import request, redirect
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'POST':
task_content = request.form['content']
new_task = ToDo(content=task_content)
try:
db.session.add(new_task)
db.session.commit()
return redirect('/')
except:
return 'There was an issue adding your task'
else:
tasks = ToDo.query.order_by(ToDo.date_created).all()
return render_template('index.html', tasks=tasks)
index.html
with Jinja2 for loop
{% for task in tasks %}
<tr>
<td>{{ task.content }}</td>
<td>{{ task.date_created.date() }}</td>
<td>
<a href="{{ url_for('delete', id=task.id) }}">Delete</a>
<a href="{{ url_for('update', id=task.id) }}">Update</a>
</td>
</tr>
{% endfor %}
@app.route('/delete/<int:id>')
def delete(id):
task_to_delete = ToDo.query.get_or_404(id)
try:
db.session.delete(task_to_delete)
db.session.commit()
return redirect('/')
except:
return 'There was a problem deleting that task'
update.html
<form action="{{ url_for('update', id=task.id) }}" method="POST">
<input type="text" name="content" id="content" value="{{ task.content }}">
<input type="submit" value="Update">
</form>
@app.route('/update/<int:id>', methods=['POST', 'GET'])
def update(id):
task = ToDo.query.get_or_404(id)
if request.method == 'POST':
task.content = request.form['content']
try:
db.session.commit()
return redirect('/')
except:
return 'There was an issue updating your task'
else:
return render_template('update.html', task=task)
index.html
to show message when no tasks
{% if tasks|length < 1 %}
<h2>There are no tasks, create one below:</h2>
{% else %}
<!-- Display tasks table -->
{% endif %}
heroku login
pip3 install gunicorn
pip3 freeze > requirements.txt
Procfile
web: gunicorn app:app
git init
git add . git commit -m "Initial commit"
### Deploy to Heroku
- Create Heroku app: `heroku create <app-name>`
- Push to Heroku: `git push heroku master`
- Open app: `heroku open`
### Verify Deployment
- Ensure CRUD functionalities work on Heroku-hosted app
## Conclusion
- GitHub repository available for reference
- Basic Flask CRUD app created and deployed with detailed steps