Beispiel #1
0
def index():
    """Main page of the dashboard."""
    # Check pre-rendered HTML
    rendered = data.get_rendered_page("country")
    if rendered:
        return rendered
    city = "Россия"
    main_links = filter_secondary_links(city)
    secondary_links = []
    # Getting data
    #full_data = data.get_newest_data()

    #request_file = request.files['data_file']

    #df = pd.read_csv(request.files.get('data_file'))  
    df = pd.read_csv(request.files.get('data_file'), sep = ";")  
    #fstring = request_file.read()
    #csv_dicts = [{k: v for k, v in row.items()} for row in csv.DictReader(fstring.splitlines(), skipinitialspace=True)]
    #df = pd.DataFrame.from_dict(csv_dicts)
    df["date"] = pd.to_datetime(df["date"], dayfirst=True)
    df = df.set_index(["date", "category"]).unstack(level=-1)
    full_data = df.sort_index()
    del df
    
    country_data_full, country_data = data.get_region_data(full_data, city)
    # Map plot
    borders = data.get_geo_data()
    latest_data = full_data.iloc[-1].unstack(level=-1)
    latest_diff = (full_data
                   .diff()
                   .iloc[-1]
                   .unstack(level=-1)
                   .rename(lambda x: f"{x}_diff", axis=1))
    map_data = (borders
                .join(latest_data, on="REGION")
                .join(latest_diff, on="REGION")
                .fillna(0))
    
    map_data["total_color"] = 1. + map_data["total"]
    map_plot = plotting.plot_map(map_data)
 



    # Getting Bokeh components
    script, div = components({"map": map_plot})
    # Rendering HTML
    rendered = render_template("main.html", city=city,
                               main_links=main_links,
                               secondary_links=secondary_links,
                               bokeh_script=script,
                               **div)
    #data.save_rendered_page("country", rendered)
    del map_data

    return rendered
Beispiel #2
0
def moscow():
    """Moscow-specific page."""

    # Check pre-rendered HTML
    city = "Москва"
    rendered = data.get_rendered_page(city)

    if rendered:
        return rendered

    main_links = [{
        "name": "Больницы",
        "link": "#hospitals"
    }, {
        "name": "Транспорт",
        "link": "#transport"
    }]
    secondary_links = filter_secondary_links(city)

    # Getting data
    full_data = data.get_newest_data()
    moscow_data_full, moscow_data = data.get_region_data(full_data, city)

    # Cases statistics block
    stats = []
    for category in cts.CATEGORIES[:-1]:
        style = cts.CATEGORIES_STYLES[category].copy()
        style["value"] = moscow_data[category]
        style["diff"] = moscow_data[f"{category}_diff"]
        stats.append(style)

    # Cases plots
    cases_plot = plotting.plot_region(moscow_data_full, city)
    cases_log_plot = plotting.plot_region(moscow_data_full, city, log_y=True)

    # Transport plots
    transport_data = data.get_data_by_key([cts.MSK_DIR, "transport"],
                                          sort_by="date",
                                          set_index="date")

    public_tr_plot = plotting.plot_region(
        transport_data / 100.,
        city,
        plot_cols=[tr["key"] for tr in cts.PUBLIC_TR_COLS],
        legend_map=[tr["name"] for tr in cts.PUBLIC_TR_COLS],
        legend_loc="top_right",
        dt_fmt="%d-%m-%Y %H:%S",
        fmt="{0 %}",
        set_yticks=True,
        width_policy="max",
        height=int(0.75 * cfg.MAX_MAIN_HEIGHT),
        xaxis_ticks=5,
        yrange=(-1.1, 0))
    private_tr_plot = plotting.plot_region(
        transport_data / 100.,
        city,
        plot_cols=[tr["key"] for tr in cts.PRIVATE_TR_COLS],
        legend_map=[tr["name"] for tr in cts.PRIVATE_TR_COLS],
        legend_loc="top_right",
        dt_fmt="%d-%m-%Y %H:%S",
        fmt="{0 %}",
        set_yticks=True,
        width_policy="max",
        height=int(0.75 * cfg.MAX_MAIN_HEIGHT),
        xaxis_ticks=5,
        yrange=(-1.1, 0))
    plots = {
        "cases": cases_plot,
        "cases_log": cases_log_plot,
        "public_transport": public_tr_plot,
        "private_transport": private_tr_plot
    }

    # Transport block
    transport_data_latest = transport_data.iloc[-1].to_dict()
    tr_stats = []

    for category in cts.PUBLIC_TR_COLS + cts.PRIVATE_TR_COLS:
        tr_stats.append({
            "name": category["name"].capitalize(),
            "value": transport_data_latest[category["key"]]
        })

    # Hospitals
    hospitals = []
    for hospital in cts.MSK_HOSPITALS:
        hospital_data = data.get_data_by_key(
            [cts.MSK_DIR, cts.MSK_HOSPITALS_DIR, hospital["key"]])
        hospital_plot = plotting.plot_region(
            hospital_data,
            hospital["name"],
            plot_cols=[tr["key"] for tr in hospital["fields"]],
            legend_map=[tr["name"] for tr in hospital["fields"]],
            glyphs={
                tr["key"]: tr["glyph"]
                for tr in hospital["fields"] if "glyph" in tr
            },
            alphas={
                tr["key"]: tr["alpha"]
                for tr in hospital["fields"] if "alpha" in tr
            },
            bar_bottom=0,
            alpha=0.9,
            legend_loc="top_left",
            width_policy="max",
            width=cfg.MAX_MAIN_WIDTH * 2,
            height_policy="fit",
            additional_tools=["ywheel_zoom", "ypan", "reset"])
        plots[hospital["key"]] = hospital_plot
        hospitals.append(hospital)

    # Getting Bokeh components
    script, div = components(plots)

    for hospital in hospitals:
        hospital_plot = div.pop(hospital["key"])
        hospital["plot"] = hospital_plot

    # Rendering HTML
    rendered = render_template("moscow.html",
                               city=city,
                               main_links=main_links,
                               secondary_links=secondary_links,
                               stats=stats,
                               tr_stats=tr_stats,
                               bokeh_script=script,
                               hospitals=hospitals,
                               **div)
    data.save_rendered_page("moscow", rendered)

    return rendered
Beispiel #3
0
def index():
    """Main page of the dashboard."""

    # Check pre-rendered HTML
    rendered = data.get_rendered_page("country")

    if rendered:
        return rendered

    city = "Россия"

    main_links = filter_secondary_links(city)
    secondary_links = []

    # Getting data
    full_data = data.get_newest_data()
    country_data_full, country_data = data.get_region_data(full_data, city)
    swabs_data = data.get_swabs_data()
    swabs_data = swabs_data.join(country_data_full["total"], how="outer")

    # Cases statistics block
    stats = []

    for category in cts.CATEGORIES:
        style = cts.CATEGORIES_STYLES[category].copy()
        style["value"] = country_data[category]
        style["diff"] = country_data[f"{category}_diff"]
        stats.append(style)

    # Map plot
    borders = data.get_geo_data()
    latest_data = full_data.iloc[-1].unstack(level=-1)
    latest_diff = (full_data.diff().iloc[-1].unstack(level=-1).rename(
        lambda x: f"{x}_diff", axis=1))
    map_data = (borders.join(latest_data,
                             on="REGION").join(latest_diff,
                                               on="REGION").fillna(0))

    map_data["total_color"] = 1. + map_data["total"]
    map_plot = plotting.plot_map(map_data)

    # Cases plots
    cases_plot = plotting.plot_region(country_data_full, city)
    cases_log_plot = plotting.plot_region(country_data_full, city, log_y=True)

    # Swabs plots
    swabs_plot = plotting.plot_swabs(swabs_data, city)
    swabs_log_plot = plotting.plot_swabs(swabs_data, city, x_col="total")

    # Getting Bokeh components
    script, div = components({
        "cases": cases_plot,
        "cases_log": cases_log_plot,
        "swabs": swabs_plot,
        "swabs_log": swabs_log_plot,
        "map": map_plot,
    })

    # Rendering HTML
    rendered = render_template("main.html",
                               city=city,
                               main_links=main_links,
                               secondary_links=secondary_links,
                               stats=stats,
                               bokeh_script=script,
                               **div)
    data.save_rendered_page("country", rendered)
    return rendered