Correct this function, when i apply filters lines loose their sorting and going crazy…
- User
Correct this function, when i apply filters lines loose their sorting and going crazy
def create_dash_app(df,col_axis_x, col_axis_y ,filter_columns): # Create Dash app app = dash.Dash(name)
filter_layout = [] for col in filter_columns: filter_layout.append( html.Div([ html.Label(f"Select {col}", style={'fontWeight': 'bold'}), dcc.Checklist( id=f'{col}-checklist', options=[{'label': val, 'value': val} for val in df[col].unique()], value=df[filter_columns].apply(lambda x: x.unique()[0]).values.tolist(), style={'marginBottom': '20px', 'display': 'flex', 'flexDirection': 'row', 'flexWrap': 'wrap', 'height': '20px', 'overflowY': 'auto'} ) ]) ) app.layout = html.Div([ html.Div([ html.H1("Interactive Cut Points Plot", style={'textAlign': 'center', 'color': '#003366'}), html.Div(filter_layout, style={'width': '48%', 'display': 'inline-block', 'verticalAlign': 'top'}), ], style={'padding': '20px', 'backgroundColor': '#f9f9f9', 'borderRadius': '10px', 'boxShadow': '0 4px 8px rgba(0, 0, 0, 0.1)'}), html.Div([ dcc.Graph(id='cut-points-plot') ], style={'padding': '20px'}) ], style={'maxWidth': '2000px', 'margin': 'auto'}) @app.callback( Output('cut-points-plot', 'figure'), [Input(f'{col}-checklist', 'value') for col in filter_columns] ) def update_plot(*selected_filters): filtered_df = df.copy() for col, selected_values in zip(filter_columns, selected_filters): if not selected_values: selected_values = df[col].unique() filtered_df = filtered_df[filtered_df[col].isin(selected_values)] # Sort the dataframe by the x-axis column filtered_df = filtered_df.sort_values(by=[col_axis_y, col_axis_x], ascending = False) # Debugging: Print the filtered DataFrame print(filtered_df) fig = px.line( filtered_df, x=col_axis_x, y=col_axis_y, color=filter_columns[0], # Use the first filter column for color line_group=filter_columns[1] if len(filter_columns) > 1 else None, # Use the second filter column for line group if available line_dash=filter_columns[1] if len(filter_columns) > 1 else None, # Use the second filter column for line dash if available markers=True, #text="classe_eta_azienda", color_discrete_sequence=px.colors.qualitative.Plotly, # Use a prettier color palette ) fig.update_layout( title='Cut Points by Sector', xaxis_title=col_axis_x, yaxis_title=col_axis_y, template='simple_white', # Apply a dark theme legend_title=filter_columns[0], height=600, font=dict( family="Arial, sans-serif", size=12, color="black" ) ) fig.update_traces(textposition="bottom right") return fig return app - Assistant
- User
it does not work because x axis stay freezed
- Assistant