CoolFace
Apppublic

Naazima/simple_streamlit_app

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
app.py103 linesDownload Raw Back to root
1import dash2import dash_core_components as dcc3import dash_html_components as html4from dash.dependencies import Output, Input5import dash_bootstrap_components as dbc6import pandas as pd7import plotly.express as px8 9df = pd.read_csv('politics.csv')10 11app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])12server = app.server13 14# radioItem list for the layout (long_code.py lines 13-45)15radio_list = []16for s,v in zip(['AZ','FL','GA','IA','ME','MI','NC','NV','OH','PA','TX','WI'],17               [11,29,16,6,4,16,15,6,18,20,38,10]):18    radio_list.append(19        html.Div([20            html.Label(f'{s}-{v}: ', style={'display':'inline', 'fontSize':15}),21            dcc.RadioItems(22                id=f'radiolist-{s}',23                options=[24                    {"label": "Dem", "value": "democrat"},25                    {"label": "Rep", "value": "republican"},26                    {"label": "NA", "value": "unsure"},27                ],28                value='unsure',29                inputStyle={'margin-left': '10px'},30                labelStyle={'display': 'inline-block'},31                style={'display':'inline'}32            ),33        ], style={'textAlign':'end'})34    )35print(radio_list)36 37 38# Input list for the callback (long_code.py lines 48-52)39input_list = []40for x in ['AZ','FL','GA','IA','ME','MI','NC','NV','OH','PA','TX','WI']:41    input_list.append(42        Input(component_id=f'radiolist-{x}', component_property='value')43    )44 45 46app.layout = html.Div([47    dbc.Row([48        dbc.Col(html.H1("USA Elections 2020", style={'textAlign':'center'}), width=12)49    ]),50    dbc.Row([51        dbc.Col(radio_list, xs=4, sm=4, md=4, lg=2, xl=2),52        dbc.Col(dcc.Graph(id='my-choropleth', figure={},53                          config={'displayModeBar':False}), xs=8, sm=8, md=8, lg=6, xl=6),54        dbc.Col(dcc.Graph(id='my-bar', figure={},55                          config={'displayModeBar': False}), xs=6, sm=6, md=6, lg=4, xl=4)56 57    ])58])59 60 61# must have Dash version 1.16.0 or higher62@app.callback(63    Output(component_id='my-choropleth', component_property='figure'),64    Output(component_id='my-bar', component_property='figure'),65    input_list66)67def update_graph(az, fl, ga, ia, me, mi, nc, nv, oh, pa, tx, wi):68    dff = df.copy()  # assign party to dataframe (long_code.py lines 55-57)69    for st,radio_value_chosen in zip(70            ['AZ','FL','GA','IA','ME','MI','NC','NV','OH','PA','TX','WI'],71            [az, fl, ga, ia, me, mi, nc, nv, oh, pa, tx, wi]):72        dff.loc[dff.state == st, 'party'] = radio_value_chosen73 74    # build map figure75    fig_map = px.choropleth(76        dff, locations="state", hover_name='electoral votes',77        locationmode="USA-states", color="party",78        scope="usa", color_discrete_map={'democrat': 'blue',79                                         'republican': 'red',80                                         'unsure': 'grey'})81 82    # build histogram figure83    dff = dff[dff.party != 'unsure']84    fig_bar = px.histogram(dff, x='party', y='electoral votes', color='party',85                           range_y=[0,350], color_discrete_map={'democrat': 'blue',86                                                                'republican': 'red'}87                           )88    # add horizontal line89    fig_bar.update_layout(showlegend=False, shapes=[90        dict(type='line', yref='paper',y0=0.77,y1=0.77, xref='x',x0=-0.5,x1=1.5)91    ])92    # add annotation text above line93    fig_bar.add_annotation(x=0.5, y=280, showarrow=False, text="270 votes to win")94 95    return fig_map, fig_bar96 97 98if __name__ == '__main__':99    app.run_server(debug=False)100 101    102 # https://youtu.be/my1nshz1uG4103