James McCool commited on
Commit
4d6e5f5
·
1 Parent(s): def2aed

Add PDF export functionality in app.py for marketshares data

Browse files

- Introduced a new function `convert_df_to_pdf` to enable PDF export of dataframes.
- Added a download selection option for users to choose between CSV and PDF formats for marketshares data.
- Updated the download button to handle PDF exports, enhancing the data export capabilities of the application.

Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -98,6 +98,9 @@ def init_baselines():
98
  def convert_df_to_csv(df):
99
  return df.to_csv().encode('utf-8')
100
 
 
 
 
101
  matchups, overall_ms, team_frame, team_list, team_dict = init_baselines()
102
 
103
  col1, col2 = st.columns([1, 9])
@@ -118,20 +121,24 @@ with col2:
118
  display_table = matchups
119
  display_table = display_table.set_index('Team')
120
  st.dataframe(display_table.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(matchups_format, precision=2), height=500, use_container_width = True)
121
- st.download_button(
122
- label="Export Matchups",
123
- data=convert_df_to_csv(display_table),
124
- file_name='Matchups_export.csv',
125
- mime='text/csv',
126
- )
127
  elif split_var1 == 'Line Marketshares':
128
  display_table = overall_ms
129
  display_parsed = display_table[display_table['Line'].str.contains('|'.join(team_split))]
130
  display_parsed = display_parsed.set_index('Line')
131
  st.dataframe(display_parsed.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(percentages_format, precision=2), height=500, use_container_width = True)
 
 
 
 
 
 
 
 
 
 
132
  st.download_button(
133
- label="Export Marketshares",
134
- data=convert_df_to_csv(display_table),
135
- file_name='Marketshares_export.csv',
136
- mime='text/csv',
137
  )
 
98
  def convert_df_to_csv(df):
99
  return df.to_csv().encode('utf-8')
100
 
101
+ def convert_df_to_pdf(df):
102
+ return df.to_pdf().encode('utf-8')
103
+
104
  matchups, overall_ms, team_frame, team_list, team_dict = init_baselines()
105
 
106
  col1, col2 = st.columns([1, 9])
 
121
  display_table = matchups
122
  display_table = display_table.set_index('Team')
123
  st.dataframe(display_table.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(matchups_format, precision=2), height=500, use_container_width = True)
 
 
 
 
 
 
124
  elif split_var1 == 'Line Marketshares':
125
  display_table = overall_ms
126
  display_parsed = display_table[display_table['Line'].str.contains('|'.join(team_split))]
127
  display_parsed = display_parsed.set_index('Line')
128
  st.dataframe(display_parsed.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(percentages_format, precision=2), height=500, use_container_width = True)
129
+ download_var = st.selectbox('What download would you like?', options = ['CSV', 'PDF'], key='download_var')
130
+ if st.button('Download'):
131
+ if download_var == 'CSV':
132
+ st.download_button(
133
+ label="Export Marketshares (CSV)",
134
+ data=convert_df_to_csv(display_table),
135
+ file_name='Marketshares_export.csv',
136
+ mime='text/csv',
137
+ )
138
+ elif download_var == 'PDF':
139
  st.download_button(
140
+ label="Export Marketshares (PDF)",
141
+ data=convert_df_to_pdf(display_table),
142
+ file_name='Marketshares_export.pdf',
143
+ mime='application/pdf',
144
  )