Skip to content

Commit 86e170e

Browse files
Merge pull request #210 from nickcanz/download_full_table_data
Add a link to download full tabular data as CSV
2 parents 28304a6 + eed1cf6 commit 86e170e

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/app.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import streamlit as st # type: ignore
55

66
from penn_chime.presentation import (
7+
build_download_link,
78
display_header,
89
display_sidebar,
910
draw_census_table,
@@ -56,6 +57,11 @@
5657
draw_projected_admissions_table(st, admissions_df, as_date=p.as_date, daily_count=True)
5758
else:
5859
draw_projected_admissions_table(st, admissions_df, as_date=p.as_date, daily_count=False)
60+
build_download_link(st,
61+
filename="projected_admissions.csv",
62+
df=admissions_df,
63+
parameters=p
64+
)
5965
st.subheader("Admitted Patients (Census)")
6066
st.markdown(
6167
"Projected **census** of COVID-19 patients, accounting for arrivals and discharges at Penn hospitals"
@@ -70,6 +76,12 @@
7076
draw_census_table(st, admissions_df, as_date=p.as_date, daily_count=True)
7177
else:
7278
draw_census_table(st, census_df, as_date=p.as_date, daily_count=False)
79+
build_download_link(st,
80+
filename="projected_census.csv",
81+
df=census_df,
82+
parameters=p
83+
)
84+
7385
st.markdown(
7486
"""**Click the checkbox below to view additional data generated by this simulation**"""
7587
)

src/penn_chime/presentation.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pandas as pd # type: ignore
88

99
from .defaults import Constants, RateLos
10-
from .utils import add_date_column
10+
from .utils import add_date_column, dataframe_to_base64
1111
from .parameters import Parameters
1212

1313
DATE_FORMAT = "%b, %d" # see https://strftime.org
@@ -446,3 +446,17 @@ def draw_raw_sir_simulation_table(st, parameters):
446446
)
447447

448448
st.table(infect_table)
449+
build_download_link(st,
450+
filename="raw_sir_simulation_data.csv",
451+
df=projection_area,
452+
parameters=parameters
453+
)
454+
455+
def build_download_link(st, filename: str, df: pd.DataFrame, parameters: Parameters):
456+
if parameters.as_date:
457+
df = add_date_column(df, drop_day_column=True, date_format="%Y-%m-%d")
458+
459+
csv = dataframe_to_base64(df)
460+
st.markdown("""
461+
<a download="{filename}" href="data:file/csv;base64,{csv}">Download full table as CSV</a>
462+
""".format(csv=csv,filename=filename), unsafe_allow_html=True)

src/penn_chime/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from collections import namedtuple
44
from datetime import datetime, timedelta
55
from typing import Optional
6+
from base64 import b64encode
67

78
import numpy as np # type: ignore
89
import pandas as pd # type: ignore
@@ -65,3 +66,15 @@ def add_date_column(
6566
df = df[date_columns + non_date_columns]
6667

6768
return df
69+
70+
def dataframe_to_base64(df: pd.DataFrame) -> str:
71+
"""Converts a dataframe to a base64-encoded CSV representation of that data.
72+
73+
This is useful for building datauris for use to download the data in the browser.
74+
75+
Arguments:
76+
df: The dataframe to convert
77+
"""
78+
csv = df.to_csv(index=False)
79+
b64 = b64encode(csv.encode()).decode()
80+
return b64

0 commit comments

Comments
 (0)