简体   繁体   中英

Multiple filter data dropdown table with plotly in python

I am currently trying to generate a table with multiple dropdown options (in a Jupiter Notebook). I have been able to create the scenario but the dropdown buttons work independently, meaning that if I select one dropdown from my 'Decision' dropdown with option 'A' and then select the other downtown 'Sex' with option 'Female' I either get option A or option 2 depending on the button last selected. However, what I really want is to have both filters applied at the same time. So I would get all Decisions 'A' that are 'Female' as filters for my table.

This is my code:

fig = go.Figure(go.Table(header={"values": df_dash.columns,'fill_color':'navy','align':'left',
                                 'font':dict(color='white', size=12)}, 
                         cells={"values": df_dash.T.values,'fill_color':'white','align':'left'}))
fig.update_layout(
    updatemenus=[
        {
            "y": 1 - (i / 5),
            "buttons": [
                {
                    "label": c,
                    "method": "restyle",
                    "args": [
                        {
                            "cells": {
                                "values": df_dash.T.values
                                if c == "All"
                                else df_dash.loc[df_dash[menu].eq(c)].T.values
                            }
                        }
                    ],
                }
                for c in ["All"] + df_dash[menu].unique().tolist()
                
            ],
            
        }
        for i, menu in enumerate(["Decision", "Sex",])
    ]
)

And this is how it looks:

表视图

Any ideas what am I doing wrong or what can I add to make both buttons actively filtering my df table at the same time?

Based on this answer in the plotly forums, you cannot create buttons that are dependent on each other.

The best you could probably do is create one dropdown with every possible combination in your current Decision and Sex dropdowns – which I understand probably isn't ideal.

If you are willing to use plotly-dash , then it is possible as you can create two dropdowns that are inputs to a basic callback that updates the fig, as shown in this example in the plotly-dash documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM