-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_reports.py
109 lines (88 loc) · 3.74 KB
/
compile_reports.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import jinja2
import pandas as pd
import bokeh
import markdown2
from jinja2 import Environment, FileSystemLoader
from bokeh.plotting import figure
from bokeh.embed import components
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn
from datetime import date
from random import randint
from bokeh.models import ColumnDataSource
from generate_summary import get_result
def get_bokeh_component():
plot = figure()
plot.circle([1,2], [3,4])
script, div = components(plot)
data = dict(
dates=[date(2014, 3, i + 1) for i in range(10)],
downloads=[randint(0, 100) for i in range(10)],
)
source = ColumnDataSource(data)
columns = [
TableColumn(field="dates", title="Date", formatter=DateFormatter()),
TableColumn(field="downloads", title="Downloads"),
]
data_table = DataTable(source=source, columns=columns, width=400, height=280)
script2, div2 = components(data_table)
return script+script2, div, div2
def get_variable(config):
from bokeh.embed import components
name, rank, df, decade_df = get_result(config.gender, config.decade, config.name)
if config.name <> name:
message = "%s not found. Do you mean %s?" % (config.name, name)
else:
message =""
birth_table = df.pivot(index='Decade', columns='Name', values='Births').fillna(0).to_html()
rank_table = df.pivot(index='Decade', columns='Name', values='Rank').fillna(0).to_html()
result_table = df[df['Rank']==rank][['Decade','Name','Rank']].sort('Decade').to_html(index=False)
top_table = decade_df[(decade_df['Rank']<=8) &
(decade_df['Decade']==config.decade)].sort('Rank').to_html(index=False)
from bokeh.charts import Line, save, output_file, ColumnDataSource
from bokeh.resources import INLINE
plot_path = "%s_%s_%i.html" % (config.name,config.gender,config.decade)
output_file("output/" + plot_path, mode='inline')
tooltips = [(c, '@' + c) for c in df.columns]
p = Line(df, x='Decade', y='Rank', title="Rank across Time", color='Name',
xlabel="Decade", ylabel="Rank",
tooltips=tooltips)
p.circle('Decade', 'Rank', color='gray', alpha=0.5, source=ColumnDataSource(df))
save(p)
#script, div = components(p)
return {
'plot_path': plot_path,
'result_table': result_table,
'rank_table': rank_table,
'birth_table': birth_table,
'top_table': top_table,
'rank': rank,
'config': config,
'name': name,
'message': message
}
import collections
Config = collections.namedtuple('BirthConfig', 'name decade gender')
configs = [Config('Chris',1980,'M'),
#Config('Wes', 1980, 'M'),
#Config('John', 1980, 'M'),
]
results = [get_variable(i) for i in configs]
# Load jinja2 template environment
env = Environment(loader=FileSystemLoader('templates/'))
env.filters['markdown'] = lambda text: (markdown2.markdown(text))
script, div, div2 = get_bokeh_component()
df = pd.DataFrame({'a':[1,2,3], 'b':[3,4,5]})
templateVars = {
"names_data": pd.read_csv("./data/names_cleaned.csv").head().to_html(),
"names_by_decade": pd.read_csv("./data/names_by_decade.csv").head().to_html(),
"results": results,
"title_variable":'This title is inserted by Jinja2 template.',
"rank": results[0]['rank'],
"result_table": results[0]['result_table'],
"plot_path": results[0]['plot_path']
}
template = env.get_template('content.j2')
parsed_result = template.render(templateVars)
import io
with io.open("output/presentation.html", "w", encoding='utf-8') as fh:
fh.write(parsed_result)