File tree Expand file tree Collapse file tree 3 files changed +40
-1
lines changed Expand file tree Collapse file tree 3 files changed +40
-1
lines changed Original file line number Diff line number Diff line change 4
4
import streamlit as st # type: ignore
5
5
6
6
from penn_chime .presentation import (
7
+ build_download_link ,
7
8
display_header ,
8
9
display_sidebar ,
9
10
draw_census_table ,
53
54
54
55
if st .checkbox ("Show Projected Admissions in tabular form" ):
55
56
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
+
56
63
st .subheader ("Admitted Patients (Census)" )
57
64
st .markdown (
58
65
"Projected **census** of COVID-19 patients, accounting for arrivals and discharges at Penn hospitals"
64
71
st .markdown (chart_descriptions (census_chart , suffix = " Census" ))
65
72
if st .checkbox ("Show Projected Census in tabular form" ):
66
73
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
+ )
67
79
st .markdown (
68
80
"""**Click the checkbox below to view additional data generated by this simulation**"""
69
81
)
Original file line number Diff line number Diff line change 7
7
import pandas as pd # type: ignore
8
8
9
9
from .defaults import Constants , RateLos
10
- from .utils import add_date_column
10
+ from .utils import add_date_column , dataframe_to_base64
11
11
from .parameters import Parameters
12
12
13
13
DATE_FORMAT = "%b, %d" # see https://strftime.org
@@ -440,3 +440,17 @@ def draw_raw_sir_simulation_table(st, parameters):
440
440
)
441
441
442
442
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 )
Original file line number Diff line number Diff line change 3
3
from collections import namedtuple
4
4
from datetime import datetime , timedelta
5
5
from typing import Optional
6
+ from base64 import b64encode
6
7
7
8
import numpy as np # type: ignore
8
9
import pandas as pd # type: ignore
@@ -65,3 +66,15 @@ def add_date_column(
65
66
df = df [date_columns + non_date_columns ]
66
67
67
68
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
You can’t perform that action at this time.
0 commit comments