[Python] Is there any way to run a Dash app in Kaggle?

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    I've been working on a project involving Dash from plotly. I made small apps with jupyter-dash locally and I wanted to share my work on Kaggle but I can't run a dash app on Kaggle since I need a localhost. I didn't find any information about this issue so I'm coming here hoping that someone faced the same issue and solved it.

    Thank you, Miljan.

    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')

    Continue reading...

Compartilhe esta Página