Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import altair as alt
|
4 |
+
|
5 |
+
# Load hospital dataset
|
6 |
+
hospitals_df = pd.read_csv("https://data.medicare.gov/api/views/xubh-q36u/rows.csv?accessType=DOWNLOAD")
|
7 |
+
|
8 |
+
# Filter for the largest hospital in each state
|
9 |
+
largest_hospitals_df = hospitals_df.loc[hospitals_df.groupby("State")["Hospital overall rating"].idxmax()]
|
10 |
+
|
11 |
+
# Select columns to display
|
12 |
+
cols_to_display = ["State", "Hospital Name", "City", "Zip Code", "lat", "lng"]
|
13 |
+
|
14 |
+
# Create a Streamlit table to display the largest hospitals
|
15 |
+
st.table(largest_hospitals_df[cols_to_display])
|
16 |
+
|
17 |
+
# Define chart functions
|
18 |
+
def stacked_bar_chart():
|
19 |
+
chart = alt.Chart(largest_hospitals_df).mark_bar().encode(
|
20 |
+
x=alt.X('State:N'),
|
21 |
+
y=alt.Y('count():Q', stack="normalize"),
|
22 |
+
color=alt.Color('Hospital Type:N'),
|
23 |
+
tooltip=['Hospital Name', 'City', 'Hospital overall rating']
|
24 |
+
).properties(
|
25 |
+
width=700,
|
26 |
+
height=400,
|
27 |
+
title='Number of Hospitals by State and Type'
|
28 |
+
)
|
29 |
+
text = chart.mark_text(
|
30 |
+
align='center',
|
31 |
+
baseline='middle',
|
32 |
+
dx=0,
|
33 |
+
dy=5,
|
34 |
+
color='white'
|
35 |
+
).encode(
|
36 |
+
text=alt.Text('count():Q', format='.1f')
|
37 |
+
)
|
38 |
+
st.altair_chart(chart + text)
|
39 |
+
|
40 |
+
def bump_chart():
|
41 |
+
chart = alt.Chart(largest_hospitals_df).transform_rank(
|
42 |
+
'Hospital overall rating',
|
43 |
+
groupby=['State']
|
44 |
+
).mark_line().encode(
|
45 |
+
x='rank:Q',
|
46 |
+
y=alt.Y('Hospital Name:N', sort='-x'),
|
47 |
+
color=alt.Color('State:N'),
|
48 |
+
tooltip=['City', 'Zip Code', 'Hospital overall rating']
|
49 |
+
).properties(
|
50 |
+
width=700,
|
51 |
+
height=400,
|
52 |
+
title='Hospital Rankings by State'
|
53 |
+
)
|
54 |
+
st.altair_chart(chart)
|
55 |
+
|
56 |
+
# Define chart buttons
|
57 |
+
st.sidebar.header('Select a Chart')
|
58 |
+
chart_options = ['Stacked Bar Chart with Text Overlay', 'Bump Chart']
|
59 |
+
chart_choice = st.sidebar.selectbox('', chart_options)
|
60 |
+
|
61 |
+
# Call chart functions based on user input
|
62 |
+
if chart_choice == 'Stacked Bar Chart with Text Overlay':
|
63 |
+
stacked_bar_chart()
|
64 |
+
elif chart_choice == 'Bump Chart':
|
65 |
+
bump_chart()
|