Skip to content
Snippets Groups Projects
Verified Commit 40a8f351 authored by Owen Graham's avatar Owen Graham
Browse files

Use the same routes for all pages and plots

parent 97843dba
No related branches found
No related tags found
No related merge requests found
......@@ -33,11 +33,11 @@
</div>
<div>
<input type="checkbox" id="check-overlay" onclick="location.href = '{{ url_for('overlay') }}';">
<input type="checkbox" id="check-overlay" onclick="location.href = '{{ url_for('page', plot_name='overlay') }}';">
<label for="check-overlay">Overlay second dataset</label>
</div>
<div>
<input type="checkbox" id="check-boxplot" checked onclick="location.href = '{{ url_for('home') }}';">
<input type="checkbox" id="check-boxplot" checked onclick="location.href = '{{ url_for('page', plot_name='index') }}';">
<label for="check-boxplot">Boxplot multiple years</label>
</div>
</div>
......
......@@ -28,11 +28,11 @@
</div>
<div>
<input type="checkbox" id="check-overlay" onclick="location.href = '{{ url_for('overlay') }}';">
<input type="checkbox" id="check-overlay" onclick="location.href = '{{ url_for('page', plot_name='overlay') }}';">
<label for="check-overlay">Overlay second dataset</label>
</div>
<div>
<input type="checkbox" id="check-boxplot" onclick="location.href = '{{ url_for('boxplot') }}';">
<input type="checkbox" id="check-boxplot" onclick="location.href = '{{ url_for('page', plot_name='boxplot') }}';">
<label for="check-boxplot">Boxplot multiple years</label>
</div>
</div>
......
......@@ -28,11 +28,11 @@
</div>
<div>
<input type="checkbox" id="check-overlay" checked onclick="location.href = '{{ url_for('home') }}';">
<input type="checkbox" id="check-overlay" checked onclick="location.href = '{{ url_for('page', plot_name='index') }}';">
<label for="check-overlay">Overlay second dataset</label>
</div>
<div>
<input type="checkbox" id="check-boxplot" onclick="location.href = '{{ url_for('boxplot') }}';">
<input type="checkbox" id="check-boxplot" onclick="location.href = '{{ url_for('page', plot_name='boxplot') }}';">
<label for="check-boxplot">Boxplot multiple years</label>
</div>
......
......@@ -35,22 +35,23 @@ plt.rcParams['axes.xmargin'] = 0
matplotlib.use('Agg')
@app.route('/')
def home():
populate_g()
return render_template('index.html')
@app.route('/', defaults={'plot_name': 'index'})
@app.route('/<plot_name>')
def page(plot_name):
"""Render a visualizer page."""
if plot_name not in plotters:
abort(404)
stations = read_stations()
g.stations = stations
@app.route('/overlay')
def overlay():
populate_g()
return render_template('overlay.html')
def id_years(station):
years = [record['year'] for record in station['records']]
return station['id'], years
g.record_years = dict(map(id_years, stations))
@app.route('/boxplot')
def boxplot():
populate_g()
return render_template('boxplot.html')
return render_template(f'{plot_name}.html')
@app.route('/record/link')
......@@ -61,8 +62,32 @@ def record_link():
return jsonify(get_link(station, year))
@app.route('/plot')
def plot():
@app.route('/plot', defaults={'plot_name': 'index'})
@app.route('/plot/<plot_name>')
def plot(plot_name):
"""Create a data plot image."""
plotter = plotters.get(plot_name)
if plotter is None:
abort(404)
return plotter.plot()
Plotter = make_dataclass('Plotter', ['name', 'plot'])
plotters = {}
def add_plotter(name):
"""Decorator to register a plot type."""
def decorator(f):
plotters[name] = Plotter(name, f)
return f
return decorator
@add_plotter('index')
def plot_index():
"""Plot one station/year/measurement."""
station_id = get_param('station')
year = get_param('year', to=year_type)
......@@ -94,7 +119,7 @@ def plot():
return savefig_response(fig)
@app.route('/plot/overlay')
@add_plotter('overlay')
def plot_overlay():
"""Plot two station/years @ one measurement."""
num_datasets = 2
......@@ -152,7 +177,7 @@ def plot_overlay():
return savefig_response(fig)
@app.route('/plot/boxplot')
@add_plotter('boxplot')
def plot_boxplot():
"""Boxplot one station/measurement @ multiple years."""
station_id = get_param('station')
......@@ -240,18 +265,6 @@ def savefig_response(fig):
return Response(buf.getvalue(), mimetype='image/png')
def populate_g():
"""Populate `g` with data for rendering page templates."""
stations = read_stations()
g.stations = stations
def id_years(station):
years = [record['year'] for record in station['records']]
return station['id'], years
g.record_years = dict(map(id_years, stations))
def read_stations():
"""Read amrdcrecords.json."""
with open('amrdcrecords.json') as f:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment