'Int' object is not subscriptable

Hi guys,

I am trying to delete rows of my Datatable with a slider, it is working well when I decrease the value of the slider but not when I increase the value of the slider:

here is the code:

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
import matplotlib.pyplot as plt
import dash
import dash_table
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
import scipy as sp
from scipy.optimize import fsolve
from scipy.interpolate import interp1d

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.SOLAR],
                meta_tags=[{'name': 'viewport',
                            'content': 'width=device-width, initial-scale=1.0'}
                          ]
               )


df = pd.read_csv('bijour.csv')

app.layout = dbc.Container([
    
    dcc.Slider(
                        id='Crediting_period',
                        marks={
                            10: '10 years',
                            30: '30 years',
                            },
                        step=1,
                        tooltip= {'always_visible': True},
                        min=10,
                        max=30,
                        value=30,
                        dots=False,
                        vertical = True,
                        updatemode='drag'
                    ),                    
    
    dcc.Graph(
        id='our_graph'),
    
    dash_table.DataTable(
                    id='table',
                    columns=[{"name":i, "id":i} for i in df.columns],
                    data=df.to_dict('records'),
                    editable=True
                    
    )
]
)
@app.callback(
    Output('table', 'data'),
    Input('Crediting_period', 'value'),
    State('table', 'data'),
)

def update_data_table(rows, records):
    columns=len([{"name":i, "id":i, "type": "numeric"} for i in df.columns]),
    change = rows - len(records)
    if change > 0:
        for i in range(change):
            records.append({c["id"]: "" for c in columns})
    elif change < 0:
        records = records[:rows]
    
        
    return records
    
    
    

@app.callback(
    Output(component_id='our_graph', component_property='figure'),
    Input(component_id='table', component_property='data')
)

def update_graph(rows):
    
    dff = pd.DataFrame(rows)
    
    
    fig = px.line(dff, x=(dff.columns[0]), y=dff.columns[1])
    
    
    return fig     


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

The csv file is :slight_smile: x,tCO2_sequestrated
1,2500
2,2500
3,2500
4,2500
5,2500
6,2500
7,2500
8,2500
9,2500
10,2500
11,2500
12,2500
13,2500
14,2500
15,2500
16,2500
17,2500
18,2500
19,2500
20,2500
21,2500
22,2500
23,2500
24,2500
25,2500
26,2500
27,2500
28,2500
29,2500

the error message is
records.append({c[“id”]: “” for c in columns})
TypeError: ‘int’ object is not subscriptable

Do you guys know how to fix that ?
Thank you very much

Look at how you define your variable columns:

Having a comma at the end of the line makes it a tuple with 1 element, eg (10,) or any other number.
I don’t quite know what you’re aiming to do, but you probably want to drop the len() and the comma at the end.