def make_territories_ref_list(territory_key, territories): """ create the link page for each state """ territories = sorted(territories) d = {'country': 'countries', 'state': 'states'} if territory_key == 'state': path = 'states_list.html' h1_name = 'States' else: path = 'countries_list.html' h1_name = 'Countries' t = ENV.get_template('territories_ref.html') t = t.render(title='By {k}'.format(k=territory_key), date=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), h1_name=h1_name, territories=[ (d[territory_key] + '/' + common.tidy_name(x) + '.html', x) for x in territories ]) if not os.path.isdir('html_temp'): os.mkdir('html_temp') print(path) with open(os.path.join('html_temp', path), 'w') as write_obj: write_obj.write(t)
def all_territories(df_week, df_day, territory_key, window = 3, plot_height = 550, plot_width = 550, verbose = False): """ Create all the HTML files for the states """ territories = list(set(df_week[territory_key])) make_territories_ref_list(territory_key, territories) dir_path = make_territories_dir(territory_key) for i in territories: if verbose: print('working on {territory}'.format(territory = i)) min_value = 10 df_ = df_week[(df_week[territory_key]==i)] p1 = common.bar_over_time(df_, key = 'deaths', plot_height = plot_height, plot_width = plot_width, title = 'Deaths by Week', line_width = 10, ignore_last = True) p6 = common.bar_over_time(df_, key = 'cases', plot_height = plot_height, plot_width = plot_width, title = 'Cases by Week', line_width = 10, ignore_last = True) p2 = common.incidents_over_time_bar(df_day[df_day[territory_key] == i], key = 'deaths', window= 3, plot_height = plot_height, plot_width = plot_width, title = 'Deaths by Day', line_width = 2) p5 = common.incidents_over_time_bar(df_day[df_day[territory_key] == i], key = 'cases', window= 3, plot_height = plot_height, plot_width = plot_width, title = 'Cases by Day', line_width = 2) death_ro, death_double_rate, p3 = dy_dx(territory_key = territory_key, territory = i, df = df_day, window = window, key = 'deaths', plot_height = 300, plot_width = 300) cases_ro, cases_double_rate, p4 = dy_dx(territory_key = territory_key, territory = i, df = df_day, window = window, key = 'cases', plot_height = 300, plot_width = 300) grid = gridplot([p1, p2, p6, p5, p3, p4], ncols = 2) script, div = components(grid) html = get_html(territory = i, script = script, div = div, death_ro = death_ro, cases_ro = cases_ro, death_double_rate = death_double_rate, cases_double_rate = cases_double_rate) with open(os.path.join(dir_path, '{territory}.html'.format( territory = common.tidy_name(i))), 'w') as write_obj: write_obj.write(html)
def make_state_graphs(verbose=False, plot_height=400, plot_width=400): if not os.path.isdir('html_temp'): os.mkdir('html_temp') date = datetime.datetime.now() df_deaths = get_data_deaths() df_cases = get_data_cases() df_day = get_state_data_day() dir_path = make_territories_dir('state') for state in set(df_deaths['state']): if verbose: print('working on {state}'.format(state=state)) ps = [] for the_info in [( df_deaths, 'death', df_day, 'deaths', 'Deaths by Week', 'Deaths by Day', ), (df_cases, 'cases', df_day, 'cases', 'Cases by Week', 'Cases by Day')]: df = the_info[0] df_day_ = the_info[2] rt_death, rt_death2 = common.get_rt( df_day_[df_day_['state'] == state]['deaths'], 7, 7) rt_cases, rt_cases2 = common.get_rt( df_day_[df_day_['state'] == state]['cases'], 7, 7) the_dict = {'dates': sorted(list(set(df['dates'].tolist())))} for i in range(1, 5): shape_data( df, state, i, the_dict, key=the_info[1], ) the_dict = _trim_data(the_dict) p = common.graph_stacked(data=the_dict, start=0, plot_height=plot_height, plot_width=plot_width, line_width=10, title=the_info[4]) p_day = common.incidents_over_time_bar( df_day_[df_day_['state'] == state], key=the_info[3], window=3, plot_height=plot_height, plot_width=plot_width, title=the_info[5], line_width=2) ps.append(p) ps.append(p_day) grid = gridplot(ps, ncols=2) script, div = components(grid) html = get_html(territory=state, script=script, div=div, date=date, rt_cases=rt_cases, rt_death=rt_death) tt = '{territory}'.format(territory=common.tidy_name(state)) + '.html' with open(os.path.join(dir_path, tt), 'w') as write_obj: write_obj.write(html) make_territories_ref_list('state', list(set(df_day['state'])))