Flask+HTSQL code
app.py:
templates/departments.html:
templates/hardcourses.html:
from htsql import HTSQL
import flask
app = flask.Flask(__name__)
htsql = HTSQL('pgsql:uni') # customize me
def data(qry):
filters = flask.request.url.replace(flask.request.base_url, '', 1)
if filters and ('?' in qry): # this can be fooled by a filtered subquery
filters = '&%s' % filters.strip()[1:]
qry = '%s%s' % (qry, filters or '')
return htsql.produce(qry)
@app.route('/departments')
def departments():
qry = '/department{school:=school.name, name}'
return flask.render_template('departments.html', data=data(qry))
@app.route('/hardcourses')
def hardcourses():
qry = '/course{dept:=department.name, *}?credits>3'
return flask.render_template('hardcourses.html', data=data(qry))
if __name__ == '__main__':
app.run(debug=True)
templates/departments.html:
<html>
<head><title>Departments</title>
<script src="static/sorttable.js"></script>
</head>
<body>
<table border="1">
<tr><th>School</th><th>Department</th></tr>
{% for row in data %}
<tr><td>{{ row.school }}</td><td>{{ row.name }}</td></tr>
{% endfor %}
</table>
</body>
</html>
templates/hardcourses.html:
<html>
<head><title>Courses</title></head>
<body>
<table border="1">
<tr><th>Department</th><th>#</th><th>Title</th></td>
{% for row in data %}
<tr>
<td><a href="/departments?code='{{ row.department_code }}'">
{{ row.dept }}</td>
<td>{{ row.no }}</td> <td>{{ row.title }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>

0 Comments:
Post a Comment
<< Home