Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Simon Willison
committed
Oct 23, 2017
1 parent
ac9d668
commit de04d7a
Showing
5 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,3 +99,6 @@ ENV/ | |
|
||
# mypy | ||
.mypy_cache/ | ||
|
||
# SQLite databases | ||
*.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM python:3 | ||
COPY . /app | ||
WORKDIR /app | ||
RUN pip install -r requirements.txt | ||
EXPOSE 8006 | ||
CMD ["python", "app.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from sanic import Sanic | ||
from sanic import response | ||
from sanic_jinja2 import SanicJinja2 | ||
import sqlite3 | ||
import json | ||
|
||
app = Sanic(__name__) | ||
jinja = SanicJinja2(app) | ||
|
||
#conn = sqlite3.connect('file:flights.db?immutable=1', uri=True) | ||
conn = sqlite3.connect('file:northwind.db?immutable=1', uri=True) | ||
conn.row_factory = sqlite3.Row | ||
|
||
|
||
@app.route('/') | ||
async def index(request, sql=None): | ||
sql = sql or request.args.get('sql', '') | ||
if not sql: | ||
sql = 'select * from sqlite_master' | ||
rows = conn.execute(sql) | ||
headers = [r[0] for r in rows.description] | ||
return jinja.render('index.html', request, | ||
headers=headers, | ||
rows=list(rows), | ||
) | ||
|
||
|
||
@app.route('/<table:[a-zA-Z0-9].*>.json') | ||
async def table_json(request, table): | ||
sql = 'select * from {} limit 20'.format(table) | ||
return response.json([ | ||
dict(r) for r in conn.execute(sql) | ||
]) | ||
|
||
|
||
@app.route('/<table:[a-zA-Z0-9].*>') | ||
async def table(request, table): | ||
sql = 'select * from {} limit 20'.format(table) | ||
return await index(request, sql) | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(host="0.0.0.0", port=8006) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
sanic==0.6.0 | ||
sanic-jinja2==0.5.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<style> | ||
td { | ||
white-space: pre; | ||
vertical-align: top; | ||
border-top: 1px solid #666; | ||
padding: 2px 4px; | ||
} | ||
</style> | ||
<table> | ||
<tr> | ||
{% for header in headers %}<th scope="col">{{ header }}</th>{% endfor %} | ||
</tr> | ||
{% for row in rows %} | ||
<tr> | ||
{% for td in row %} | ||
<td>{{ td }}</td> | ||
{% endfor %} | ||
</tr> | ||
{% endfor %} | ||
</table> |