【问题标题】:How to use @app.callback input value for creating dataframe and displaying it as table如何使用@app.callback 输入值创建数据框并将其显示为表格
【发布时间】:2020-10-25 20:49:13
【问题描述】:

我正在尝试创建一个应用来计算制冷循环。我设法只显示我的结果,但我无法整合用户输入。

此时我想要实现的目标如下:

  • 用户输入:数字
  • 输入的数字用于循环计算
  • 数据框已创建
  • 数据框显示为 dash_table.DataTable(希望稍后会格式化)

我被错误困住了

dash.exceptions.InvalidCallbackReturnValue: The callback for `<Output `output_df.data`>`
            returned a value having type `DataFrame`
            which is not JSON serializable.

到目前为止,这是我的代码:

from CoolProp.CoolProp import PropsSI
import numpy as np 
import pandas as pd
import dash
import dash_core_components as dcc
import dash_table
import dash_html_components as html
from dash.dependencies import Input, Output


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
colors = {
    'background':'#283747',
    'text':'#7FDBFF'}
###App layout starts here###
app.layout = html.Div(style={'backgroundColor':colors['background']},
    children=[
    html.H1(
        children='R744 cycle',
        style={
            'textAlign':'center',
            'color':colors['text']}
        ),
    html.H2(
        children='A Dash sandbox',
        style={'textAlign':'center',
               'color':'#e74c3c'}
        ),
 #Here INPUT DATA
    html.Div(["Input: ",
              dcc.Input(id='input_data', value='25', type='number')],
         style={'color':colors['text']}
         ),
    
dash_table.DataTable(id='output_df',
    style_as_list_view=True,

),
##Maybe later: Add a fancy Slider as input 
    ##dcc.Slider(
        ##min=0,
       ## max=8,
       ## step=None,
       ## marks={
       ## 0: '20 °C',
       ## 2: '22 °C',
       ## 4: '24 °C',
       ## 6: '26 °C',
       ## 8: '28 °C'
  ##  },
   ## value=5)  
])
###App layout ends here###

###Callbacks to set calculation value for cycle###
@app.callback(
    Output(component_id='output_df', component_property='data'),
    [Input(component_id='input_data', component_property='value')]
)
def update_output_div(input_value):
     #Kältekreislauf Basic
    T_0=-10+273.15
    T_C= float(input_value) + 273.15  ##input value has to be float for caluclation! 
    eta_is = 0.7
    fluid = 'R744'
        
    delta_T_sh = 5 
    p_0 = PropsSI('P','T',T_0,'Q',0,fluid)
    p_C = PropsSI('P','T',T_C,'Q',0,fluid)
        
    states=4 
    p=np.zeros(states)
    T=np.zeros(states)
    h=np.zeros(states)
    s=np.zeros(states)
        
    #State 1 suction line
    p[0]=p_0
    T[0]=T_0 + delta_T_sh
    h[0]=PropsSI('H','T',T[0],'P',p[0],fluid)
    s[0]=PropsSI('S','T',T[0],'P',p[0],fluid)
    #State 2 Discharge line
    p[1]=p_C
    h_is_1 = PropsSI('H','S',s[0],'P',p[1],fluid)
    h[1]=(h_is_1-h[0])/eta_is+h[0]
    T[1]=PropsSI('T','H',h[1],'P',p[1],fluid)
    #State 3 After Condenser @ Bubble line
    p[2]=p_C
    h[2]=PropsSI('H','P',p[2],'Q',0,fluid)
    T[2]=PropsSI('T','P',p[2],'Q',0,fluid)
    #State 4 After expansion
    p[3]=p_0
    h[3]=h[2]
    T[3]=PropsSI('T','P',p[3],'H',h[2],fluid)


    df = pd.DataFrame (
    {
     'Pressure in bar': p*10**(-5),
     'Temperature in K':T,
     'Specific Enthalphy in kJ/kg':h/1000,     
     },
    index=['State 1','State 2','State 3','State 4']
    )
    df_rounded=df.round(3)
    return df_rounded #Result of function is a pandas dataframe!
                    
if __name__ == '__main__':
    app.run_server(debug=True)

【问题讨论】:

    标签: python pandas plotly-dash


    【解决方案1】:

    您应该将回调的输出更改为df.to_dict(orient='records')。您不能将 df 直接传递到 Dash 数据表中,这就是错误告诉您的内容。不过,它将作为dict 对象工作。您可能还想为要输出到的 data 属性设置默认值,因为 Dash 有时会对未设置的属性感到挑剔。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-26
      • 2017-05-02
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      • 1970-01-01
      相关资源
      最近更新 更多