From 5978ce65b91425fc5fdbeb38ebb9f19724496d17 Mon Sep 17 00:00:00 2001 From: bcollins Date: Tue, 3 Jan 2017 14:14:08 -0600 Subject: [PATCH] added responsive_flexbox.j2 template and render_template util function --- contrib/box_plot/main.py | 56 ++++++++++++++++++------ taxplots/templates/responsive_flexbox.j2 | 7 +++ taxplots/utils.py | 29 +++++++++++- 3 files changed, 76 insertions(+), 16 deletions(-) diff --git a/contrib/box_plot/main.py b/contrib/box_plot/main.py index 2a29e00..e331b5f 100644 --- a/contrib/box_plot/main.py +++ b/contrib/box_plot/main.py @@ -1,12 +1,18 @@ -from styles import RED, PLOT_FORMATS, DARK_GRAY + from bokeh.models import ColumnDataSource, Range1d from bokeh.models import CustomJS, RadioButtonGroup -from bokeh.plotting import figure, output_file, show -from bokeh.layouts import row, widgetbox, column +from bokeh.plotting import figure +from bokeh.layouts import column from bokeh.models.widgets import Div, RadioGroup +from bokeh.embed import components + import pandas as pd from math import pi +from taxplots.utils import render_template + +from styles import RED, PLOT_FORMATS, DARK_GRAY + # create data sources ----------------- source_csv = pd.read_csv('data.csv', parse_dates=[0]) source_csv = source_csv.rename(columns={'Unnamed: 0': 'year'}) @@ -39,10 +45,22 @@ sources['capital_gains_yes'] = ColumnDataSource(dict(labels=["0.0", "0.25", "0.4", "0.55"], source_ids=["re10", "re11", "re12", "re13"])) + # create figure ------------------ -fig = figure(x_axis_type="datetime", tools=[], responsive=True, **PLOT_FORMATS) +fig = figure(x_axis_type="datetime", + tools=[], + plot_width=400, + plot_height=400, + **PLOT_FORMATS) fig.set(y_range=Range1d(-9.5, 18.5)) -fig.vbar(x='year', top='reform', source=sources['ref'], line_width=20, bottom=0, color=RED, fill_alpha=0.4) +fig.vbar(x='year', + top='reform', + source=sources['ref'], + line_width=20, + bottom=0, + color=RED, + fill_alpha=0.4) + fig.title.align = 'center' fig.title.text_font_size = '10pt' fig.yaxis.axis_label = 'Change in individual income and payroll tax liabilities (Billions)' @@ -56,17 +74,19 @@ height=1, color=DARK_GRAY) + # create components -------------- -title_div = Div(text='Revenue Impact of a 4% Surtax on Taxpayers with Adjusted Gross Income over $5 Million', width=550, height=30) +title_div = Div(text='Revenue Impact of a 4% Surtax on Taxpayers with Adjusted Gross Income over $5 Million', height=30) -radio_group_text = Div(text="Include Additional Capital Gains Behavior.", width=280, height=18) +radio_group_text = Div(text="Include Additional Capital Gains Behavior.") radio_group = RadioGroup(labels=["Without", "With"], active=0) sources['radio_group'] = radio_group -elasticity_text = Div(text="Elasticity of Taxable Income", width=280, height=18) +elasticity_text = Div(text="Elasticity of Taxable Income") elasticity_option = RadioButtonGroup(labels=sources['capital_gains_no'].data['labels'], active=0) sources['elasticity_option'] = elasticity_option + # create callbacks --------------- radio_group.callback = CustomJS(args=sources, code=""" var capitalGainsType = radio_group.active === 0 ? capital_gains_no : capital_gains_yes; @@ -86,10 +106,18 @@ ref.trigger('change'); """) -# create layout ----------------- -grid = column(column(row(children=[widgetbox(radio_group_text), widgetbox(elasticity_text)]), - row(radio_group, elasticity_option)), - column(title_div, fig)) -output_file("index_landscape.html") -show(grid) +# create layout ----------------- +controls = column(radio_group_text, + radio_group, + elasticity_text, + elasticity_option) + +plots = dict(header=title_div, left=controls, center=fig) +script, divs = components(plots) + +template_args = dict() +template_args['bokeh_script'] = script +template_args['plots'] = divs +template_args['page_title'] = 'Box Plot' +render_template('responsive', template_args, 'index.html') diff --git a/taxplots/templates/responsive_flexbox.j2 b/taxplots/templates/responsive_flexbox.j2 index 72b9c79..6835def 100644 --- a/taxplots/templates/responsive_flexbox.j2 +++ b/taxplots/templates/responsive_flexbox.j2 @@ -44,6 +44,11 @@ +
{{ plots.header | safe }} @@ -53,6 +58,8 @@
{{ plots.left | safe}} +
+