Skip to content

Commit 1920402

Browse files
author
Nick Canzoneri
committed
Add a link to download full tabular data as CSV
1 parent c613031 commit 1920402

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,
@@ -53,6 +54,12 @@
5354

5455
if st.checkbox("Show Projected Admissions in tabular form"):
5556
draw_projected_admissions_table(st, admissions_df, as_date=p.as_date)
57+
build_download_link(st,
58+
filename="projected_admissions.csv",
59+
df=admissions_df,
60+
parameters=p
61+
)
62+
5663
st.subheader("Admitted Patients (Census)")
5764
st.markdown(
5865
"Projected **census** of COVID-19 patients, accounting for arrivals and discharges at Penn hospitals"
@@ -64,6 +71,11 @@
6471
st.markdown(chart_descriptions(census_chart, suffix=" Census"))
6572
if st.checkbox("Show Projected Census in tabular form"):
6673
draw_census_table(st, census_df, as_date=p.as_date)
74+
build_download_link(st,
75+
filename="projected_census.csv",
76+
df=census_df,
77+
parameters=p
78+
)
6779
st.markdown(
6880
"""**Click the checkbox below to view additional data generated by this simulation**"""
6981
)

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
@@ -440,3 +440,17 @@ def draw_raw_sir_simulation_table(st, parameters):
440440
)
441441

442442
st.table(infect_table)
443+
build_download_link(st,
444+
filename="raw_sir_simulation_data.csv",
445+
df=projection_area,
446+
parameters=parameters
447+
)
448+
449+
def build_download_link(st, filename: str, df: pd.DataFrame, parameters: Parameters):
450+
if parameters.as_date:
451+
df = add_date_column(df, drop_day_column=True, date_format="%Y-%m-%d")
452+
453+
csv = dataframe_to_base64(df)
454+
st.markdown("""
455+
<a download="{filename}" href="data:file/csv;base64,{csv}">Download full table as CSV</a>
456+
""".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)