Search code examples
python-3.xdataframedashboardplotly-dash

Python Dash Data Table should display only selected columns


I am trying to display only selected columns from my dataframe using datatable . i am able select how many rows i want . looking for a similar option like rows i want to select to display certain columns alone at the time of executing the code.

My dataframe has close to 25 columns . i dont want all of them to be displayed hence looking for this solution

here is my code :

import dash
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_html_components as html
import dash_table as dt
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import plotly.express as px
import pandas as pd

df = pd.read_csv('E:\pylab\dshlab\infratickets.csv', low_memory = False )

app = dash.Dash(__name__)
#style={'visibility': 'hidden'}

dpdown = []
for i in df['ASSIGNED_GROUP'].unique() :
   str(dpdown.append({'label':i,'value':(i)}))

app.layout = html.Div([
             html.P([
             html.Label("Choose a feature"),
             html.Div(dcc.Dropdown(id='dropdown', options=dpdown),
                                style = {'width': '100px',
                                    'fontSize' : '10px',
                                    'padding-left' : '100px',
                                    'display': 'inline-block'})]),


    #style={'visibility': 'hidden'},
            html.Div(id='table-container',  className='tableDiv'),
            dcc.Graph(id = 'plot',style={'height' : '25%', 'width' : '25%'})

   ])
    #dcc.Dropdown(id='dropdown', style={'height': '30px', 'width': '100px'}, options=dpdown),
    #dcc.Graph(id='graph'),
                #html.Div(html.H3('country graph'),id='table-container1',className='tableDiv1')





@app.callback(
    dash.dependencies.Output('table-container','children'),
    [dash.dependencies.Input('dropdown', 'value')])

def display_table(dpdown):
    df_temp = df[df['ASSIGNED_GROUP']==dpdown]
    return html.Div([
        dt.DataTable(
            id='main-table',
            columns=[{'name': i, 'id': i} for i in df_temp.columns],
             data=df_temp[0:5].to_dict('rows'),
             style_table={
                'maxHeight': '20%',
                #'overflowY': 'scroll',
                'width': '30%',
                'minWidth': '10%',
            },
            style_header={'backgroundColor': 'rgb(30, 30, 30)'},
            style_cell={'backgroundColor': 'rgb(50, 50, 50)','color': 'white','height': 'auto','width': 'auto'},#minWidth': '0px', 'maxWidth': '180px', 'whiteSpace': 'normal'},
            #style_cell={'minWidth': '120px', 'width': '150px', 'maxWidth': '180px'},
              style_data={'whiteSpace': 'auto','height': 'auto','width': 'auto'}

    )

    ])



if __name__ == '__main__':
    app.run_server(debug=True)

Solution

  • Able to figure out the solution

    changed the code

    columns=[{'name': i, 'id': i} for i in df_temp.columns]
    

    to

    columns=[{'name': i, 'id': i} for i in df.loc[:,['Colname1','Colname2',...]
    

    fixed it