有没有办法在Kaggle中运行Dash应用程序?

2024-05-16 20:42:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在做一个项目,涉及plotly的Dash。我在本地用jupyter dash制作了一些小应用,我想分享我在Kaggle上的工作,但我不能在Kaggle上运行dash应用,因为我需要一个本地主机。我没有找到关于这个问题的任何信息,所以我来这里希望有人能面对同样的问题并解决它

谢谢,, 米尔扬

app = JupyterDash(__name__)

year_options = []
for year in df_cleaned['Year'].unique():
    year_options.append({'label':str(year), 'value':year})
year_options.append({'label':'All', 'value':'All'})
    
segment_options = []
for segment in df_cleaned['Segment'].unique():
    segment_options.append({'label':str(segment), 'value':segment})
segment_options.append({'label':'All Customers', 'value':'All Customers'})


app.layout = html.Div([
    dcc.Graph(id='graph'),
    dcc.Dropdown(id='year-picker',
                options=year_options,
                value='All'),
    dcc.Dropdown(id='segment-picker',
                options=segment_options,
                value='All Customers')
    
    
])
@app.callback(Output(component_id='graph', component_property='figure'),
             [Input(component_id='year-picker', component_property='value'),
             Input(component_id='segment-picker', component_property='value')])
def update_figure(selected_year, selected_segment):
    
    # Data only for selected year from dropdown
    if selected_year!= 'All' and selected_segment == 'All Customers':
        filtered_df = df_cleaned[df_cleaned['Year']== int(selected_year)]
    
    elif selected_year!= 'All' and selected_segment != 'All Customers':
        filtered_df = df_cleaned[df_cleaned['Year']== int(selected_year)]
        filtered_df = filtered_df[filtered_df['Segment']==selected_segment]
        
    elif selected_year == 'All' and selected_segment == 'All Customers':
        filtered_df = df_cleaned.copy(deep=True)
    
    elif selected_year == 'All' and selected_segment != 'All Customers':
        filtered_df = df_cleaned[df_cleaned['Segment']==selected_segment]
        
        
    customer_country=filtered_df[['Country','Customer ID']].drop_duplicates()
    filtered_df_grouped = customer_country.groupby(['Country'])['Customer ID'].aggregate('count').reset_index().sort_values('Customer ID', ascending=False)
        
    data = go.Choropleth(
                locations = filtered_df_grouped['Country'],
                locationmode = 'country names',
                z = filtered_df_grouped['Customer ID'],
                text = filtered_df_grouped['Country'],
                colorscale = 'Rainbow',
                marker_line_color='darkgray',
                marker_line_width=0.5,
                colorbar_title = 'Customers',
                )

    layout = go.Layout(
                title_text='Our customers',
                title_x=0.5,
                geo=dict(
                    showframe=False,
                    showcoastlines=False,
                    projection_type='equirectangular'
                ),
                )

    fig_update = go.Figure(data=data, layout=layout)

    return fig_update        
        
app.run_server(mode='inline')


Tags: idappdfvaluesegmentallyearlabel