Permalink
Browse files

Add database support with Flask-SQLAlchemy

  • Loading branch information...
greyli committed Dec 23, 2018
1 parent f9cd575 commit 7f167d29648fe97aff92840f975e49be22f8ac26
Showing with 89 additions and 17 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 Pipfile
  3. +15 −1 Pipfile.lock
  4. +70 −14 app.py
  5. +2 −2 templates/index.html
@@ -3,3 +3,4 @@
__pycache__
.DS_Store
.env
*.db
@@ -8,3 +8,4 @@ name = "pypi"
[packages]
flask = "*"
python-dotenv = "*"
flask-sqlalchemy = "*"
@@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "e08f43beea73318bf9d74df5b322595d672f1d2a91e46f388e7390378432f44a"
"sha256": "255b4cff8b894f027fdc0cdff3975cc115aeb877b480e79bd0d8262462de533c"
},
"pipfile-spec": 6,
"requires": {},
@@ -29,6 +29,14 @@
"index": "pypi",
"version": "==1.0.2"
},
"flask-sqlalchemy": {
"hashes": [
"sha256:3bc0fac969dd8c0ace01b32060f0c729565293302f0c4269beed154b46bec50b",
"sha256:5971b9852b5888655f11db634e87725a9031e170f37c0ce7851cf83497f56e53"
],
"index": "pypi",
"version": "==2.3.2"
},
"itsdangerous": {
"hashes": [
"sha256:321b033d07f2a4136d3ec762eac9f16a10ccd60f53c0c91af90217ace7ba1f19",
@@ -84,6 +92,12 @@
"index": "pypi",
"version": "==0.10.1"
},
"sqlalchemy": {
"hashes": [
"sha256:809547455d012734b4252081db1e6b4fc731de2299f3755708c39863625e1c77"
],
"version": "==1.2.15"
},
"werkzeug": {
"hashes": [
"sha256:c3fd7a7d41976d9f44db327260e263132466836cef6f91512889ed60ad26557c",
84 app.py
@@ -1,22 +1,78 @@
# -*- coding: utf-8 -*-
import os
import sys

import click
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy

# SQLite URI compatible
WIN = sys.platform.startswith('win')
if WIN:
prefix = 'sqlite:///'
else:
prefix = 'sqlite:////'


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = prefix + os.path.join(app.root_path, 'data.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)


@app.cli.command()
@click.option('--drop', is_flag=True, help='Create after drop.')
def initdb(drop):
"""Initialize the database."""
if drop:
db.drop_all()
db.create_all()
click.echo('Initialized database.')


@app.cli.command()
def forge():
"""Generate fake data."""
db.create_all()

name = 'Grey Li'
movies = [
{'title': 'My Neighbor Totoro', 'year': '1988'},
{'title': 'Dead Poets Society', 'year': '1989'},
{'title': 'A Perfect World', 'year': '1993'},
{'title': 'Leon', 'year': '1994'},
{'title': 'Mahjong', 'year': '1996'},
{'title': 'Swallowtail Butterfly', 'year': '1996'},
{'title': 'King of Comedy', 'year': '1999'},
{'title': 'Devils on the Doorstep', 'year': '1999'},
{'title': 'WALL-E', 'year': '2008'},
{'title': 'The Pork of Music', 'year': '2012'},
]

user = User(name=name)
db.session.add(user)
for m in movies:
movie = Movie(title=m['title'], year=m['year'])
db.session.add(movie)

db.session.commit()
click.echo('Done.')


class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20))

name = 'Grey Li'
movies = [
{'title': 'My Neighbor Totoro', 'year': '1988'},
{'title': 'Dead Poets Society', 'year': '1989'},
{'title': 'A Perfect World', 'year': '1993'},
{'title': 'Leon', 'year': '1994'},
{'title': 'Mahjong', 'year': '1996'},
{'title': 'Swallowtail Butterfly', 'year': '1996'},
{'title': 'King of Comedy', 'year': '1999'},
{'title': 'Devils on the Doorstep', 'year': '1999'},
{'title': 'WALL-E', 'year': '2008'},
{'title': 'The Pork of Music', 'year': '2012'},
]

class Movie(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(60))
year = db.Column(db.String(4))


@app.route('/')
def index():
return render_template('index.html', name=name, movies=movies)
user = User.query.first()
movies = Movie.query.all()
return render_template('index.html', user=user, movies=movies)
@@ -2,14 +2,14 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{ name }}'s Watchlist</title>
<title>{{ user.name }}'s Watchlist</title>
<link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" type="text/css">
</head>
<body>
<h2>
<img alt="Avatar" class="avatar" src="{{ url_for('static', filename='images/avatar.png') }}">
{{ name }}'s Watchlist
{{ user.name }}'s Watchlist
</h2>
<p>{{ movies|length }} Titles</p>
<ul class="movie-list">

0 comments on commit 7f167d2

Please sign in to comment.