Hi guys,
I am trying to update the data table in my app and it is working well when I want to decrease the rows of the datatable, but I have an error message when I want to increase the rows.
Here is the error message I have:
and 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('data.csv')
df['cumsum'] = df['co2'].cumsum()
app.layout = dbc.Container([
html.H1("Reforestation Project and VCUs",
className='text-center mb-3'),
html.Br(),
dbc.Row(
[
dbc.Col(
[
html.Label('Crediting Period'),
dcc.Slider(
id='Crediting_period',
marks={
10: '10 years',
30: '30 years',
},
step=1,
tooltip= {'always_visible': True},
min=10,
max=30,
value=16,
dots=False,
vertical = True,
updatemode='drag'
)
],
width={'size':1}
),
dbc.Col(
dcc.Graph(
id='our_graph'),
width={'size':5}
),
dbc.Col(
dcc.Dropdown(
id='dropdown',
options=[
{'label': 'linear', 'value': 'linear'},
{'label': 'precise', 'value': 'precise'}
],
value='linear'
),
width={'size':1}
),
dbc.Col(
dash_table.DataTable(
id='table',
columns=[{"name":i, "id":i} for i in df.columns],
data=df.to_dict('records'),
editable=True,
style_table={'height': '450px'}
),
width={'size': 2, 'offset': 1}
)
],
justify='start',
no_gutters=True
),
html.Br(),
html.Br(),
dbc.Row(
[
dbc.Col(
[
html.H4("Turnover Varibables", style={'text-align': 'center'}),
html.Label('Estimated Annual Volume of CO2 sequestrated'),
dcc.Slider(
id='Estimated_Annual_tCO2',
marks={
0: '0',
5000: '5 000',
10000: '10 000',
15000: '15 000',
},
step=100,
tooltip= {'always_visible': True},
min=0,
max=15000,
value=7500,
dots=False,
updatemode='drag'),
html.Label('Price of Carbon Credits "$/tCO2"'),
dcc.Slider(
id='Price_tCO2',
marks={
0: '$0',
20: '$20'
},
step=0.5,
tooltip= {'always_visible': True},
min=0,
max=20,
value=10,
dots=False,
updatemode='drag'),
html.H4("Break Even Point", className='text-center'),
html.Div(id='intersection')
],
width={'size':3}
),
dbc.Col(
[
html.H4("Cost Varibables", style={'text-align': 'center'}),
html.Label('Hectares of Land'),
dcc.Slider(
id='Hectares_of_land',
marks={
0: '0 ha',
50: '50 ha',
100: '100 ha',
150: '150 ha',
200: '200 ha',
250: '250 ha',
300: '300 ha',
},
step=10,
tooltip= {'always_visible': True},
min=0,
max=300,
value=150,
dots=False,
updatemode='drag'),
html.Label('Variables Costs/Hectare'),
dcc.Slider(
id='Variables_costs_ha',
marks={
0: '$ 0',
1000: '$ 1000',
2000: '$ 2000',
3000: '$ 3000',
4000: '$ 5000',
5000: '$ 5000',
},
step=50,
tooltip= {'always_visible': True},
min=0,
max=5000,
value=2500,
dots=False,
updatemode='drag'),
html.Label('Gap Analysis Fee'),
dcc.Slider(
id='Gap_analysis_fee',
marks={
0: '$ 0',
20000: '$ 20 000',
},
step=100,
tooltip= {'always_visible': True},
min=0,
max=20000,
value=10000,
dots=False,
updatemode='drag'),
html.Label('Audit Fee'),
dcc.Slider(
id='Audit_fee',
marks={
0: '$ 0',
20000: '$ 20 000',
},
step=100,
tooltip= {'always_visible': True},
min=0,
max=20000,
value=10000,
dots=False,
updatemode='drag'),
],
width={'size':3},
)
],
justify='start'
)
],
fluid=True,
)
@app.callback(
Output('table', 'data'),
Input('Crediting_period', 'value'),
State('table', 'data'),
)
def update_data_table(rows, records):
columns=[{"name":i, "id":i, "type": "numeric"} for i in df.columns]
change = rows - len(records)
if records is None:
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'),
Output(component_id='intersection', component_property='children')],
[Input(component_id='Estimated_Annual_tCO2', component_property='value'),
Input(component_id='Price_tCO2', component_property='value'),
Input(component_id='Hectares_of_land', component_property='value'),
Input(component_id='Variables_costs_ha', component_property='value'),
Input(component_id='Gap_analysis_fee', component_property='value'),
Input(component_id='Audit_fee', component_property='value'),
Input(component_id='Crediting_period', component_property='value'),
Input(component_id='table', component_property='data'),
Input(component_id='dropdown', component_property='value')]
)
def update_graph(Est_ann_emissions, Price, Hectares_of_land, Var_costs_ha, Gap_fee, Audit_fee, Cred_period, rows, dropdown):
m= Cred_period
x=np.arange(0, m)
b= 8375 + Est_ann_emissions*0.1*x + 0.05*Est_ann_emissions*x + 0.02*Est_ann_emissions*x + 2500*x + Gap_fee + Audit_fee + Hectares_of_land*Var_costs_ha
dff = pd.DataFrame(rows)
if dropdown=='linear':
y=x*Est_ann_emissions*Price
fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y, name='Turnover'))
fig.add_trace(go.Scatter(x=x, y=b, name='Costs'))
bep = interp1d(y - b, x)(0)
else :
fig = px.line(dff, x=(dff.columns[0]), y=dff.columns[2])
fig.add_scatter(x=x, y=b, name='Costs')
y = fig.data[0].y
y = np.array(y)
y = list(map(int, y))
bep = interp1d(y - b, x, fill_value="extrapolate")(0)
return fig, bep
Thank you so much for your help guys !