├── flaskr
├── flaskr.db
├── schema.sql
├── populate_db.py
├── flaskr.py
└── templates
│ └── linegraph.html
├── .idea
└── vcs.xml
├── requirements.txt
├── README.md
├── .gitignore
└── LICENSE
/flaskr/flaskr.db:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/johnsliao/flask-sqlite3-chartjs-toy/HEAD/flaskr/flaskr.db
--------------------------------------------------------------------------------
/flaskr/schema.sql:
--------------------------------------------------------------------------------
1 | drop table if exists entries;
2 | create table entries (
3 | id integer primary key autoincrement,
4 | week integer,
5 | data1 integer,
6 | data2 integer
7 | );
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | Flask==0.10.1
2 | itsdangerous==0.24
3 | Jinja2==2.8
4 | MarkupSafe==0.23
5 | numpy==1.10.4
6 | pandas==0.17.1
7 | python-dateutil==2.4.2
8 | pytz==2015.7
9 | six==1.10.0
10 | Werkzeug==0.11.3
11 | wheel==0.24.0
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Playing around with chart.js, sqlite3, flask
2 |
3 | Creates simple line chart using data from sqlite3 database.
4 |
5 | # How to set up:
6 |
7 | 1. Clone repository
8 | 2. Install dependencies `pip install -r requirements.txt`
9 | 3. `python flaskr.py`
10 | 4. `python populate_db.py`
11 | 5. Open browser to `http://127.0.0.1:5000/`. You should see this:
12 |
13 | 
14 |
--------------------------------------------------------------------------------
/flaskr/populate_db.py:
--------------------------------------------------------------------------------
1 | import sqlite3
2 |
3 | conn = sqlite3.connect('flaskr.db')
4 | c = conn.cursor()
5 |
6 | c.execute('insert into entries (week, data1, data2) values (1, 5, 2)')
7 | c.execute('insert into entries (week, data1, data2) values (2, 2, 4)')
8 | c.execute('insert into entries (week, data1, data2) values (3, 9, 6)')
9 | c.execute('insert into entries (week, data1, data2) values (4, 12, 8)')
10 | c.execute('insert into entries (week, data1, data2) values (5, 20, 12)')
11 | c.execute('insert into entries (week, data1, data2) values (6, 17, 18)')
12 |
13 | conn.commit()
14 | conn.close()
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | env/
12 | build/
13 | develop-eggs/
14 | dist/
15 | downloads/
16 | eggs/
17 | .eggs/
18 | lib/
19 | lib64/
20 | parts/
21 | sdist/
22 | var/
23 | *.egg-info/
24 | .installed.cfg
25 | *.egg
26 |
27 | # PyInstaller
28 | # Usually these files are written by a python script from a template
29 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
30 | *.manifest
31 | *.spec
32 |
33 | # Installer logs
34 | pip-log.txt
35 | pip-delete-this-directory.txt
36 |
37 | # Unit test / coverage reports
38 | htmlcov/
39 | .tox/
40 | .coverage
41 | .coverage.*
42 | .cache
43 | nosetests.xml
44 | coverage.xml
45 | *,cover
46 | .hypothesis/
47 |
48 | # Translations
49 | *.mo
50 | *.pot
51 |
52 | # Django stuff:
53 | *.log
54 |
55 | # Sphinx documentation
56 | docs/_build/
57 |
58 | # PyBuilder
59 | target/
60 |
61 | #Ipython Notebook
62 | .ipynb_checkpoints
63 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 John Liao
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/flaskr/flaskr.py:
--------------------------------------------------------------------------------
1 | # all the imports
2 | import os
3 | import sqlite3
4 | import pandas as pd
5 | from flask import Flask, g, render_template
6 | from contextlib import closing
7 |
8 | # create our little application :)
9 | app = Flask(__name__)
10 |
11 | # configuration
12 | app.config.update(dict(
13 | DATABASE=os.path.join(app.root_path, 'flaskr.db'),
14 | ))
15 |
16 | def connect_db():
17 | return sqlite3.connect(app.config['DATABASE'])
18 |
19 | def init_db():
20 | with closing(connect_db()) as db:
21 | with app.open_resource('schema.sql', mode='r') as f:
22 | db.cursor().executescript(f.read())
23 | db.commit()
24 |
25 | @app.route('/')
26 | def stacked_bar_chart():
27 | # Read sqlite query results into a pandas DataFrame
28 | con = sqlite3.connect("flaskr.db")
29 | df = pd.read_sql_query("SELECT * from entries", con)
30 |
31 | # verify that result of SQL query is stored in the dataframe
32 | print(df.to_json())
33 |
34 | con.close()
35 |
36 | weeks = df['week'].values.tolist() # x axis
37 | data1 = df['data1'].values.tolist()
38 | data2 = df['data2'].values.tolist()
39 |
40 | return render_template('linegraph.html', weeks=weeks, data1=data1, data2=data2)
41 |
42 | if __name__ == '__main__':
43 | init_db()
44 | app.run(debug=True)
--------------------------------------------------------------------------------
/flaskr/templates/linegraph.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Bar Chart
5 |
6 |
7 |
8 |
9 |
44 |
45 |
--------------------------------------------------------------------------------