首页 > 解决方案 > Plotly Dash 上传组件

问题描述

我从破折号文档中获取了上传示例,并希望对其进行扩展以适应 .ifc 文件的上传(AEC 行业的 3D 模型)。我设法读取了文件,但是一旦我尝试使用 ifc python 包对文件执行某些操作,服务器就会崩溃并显示错误消息:

回调失败:服务器没有响应。(此错误源于运行 Dash 应用程序的内置 JavaScript 代码。单击以查看完整的堆栈跟踪或打开浏览器的控制台。)

我必须在哪里单击才能查看完整的堆栈跟踪?我打开 beowsers 控制台,里面什么都没有……</p>

这是代码

import base64
import datetime
import io

import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import ifcopenshell as ifc



from Functions import *

import pandas as pd


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

app = dash.Dash(__name__, external_stylesheets=external_stylesheets, prevent_initial_callbacks=True)

app.layout = html.Div([
    dcc.Upload(
        id='upload-data',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select Files')
        ]),
        style={
            'width': '100%',
            'height': '60px',
            'lineHeight': '60px',
            'borderWidth': '1px',
            'borderStyle': 'dashed',
            'borderRadius': '5px',
            'textAlign': 'center',
            'margin': '10px'
        },
        # Allow multiple files to be uploaded
        multiple=True
    ),
    html.Div(id='output-data-upload'),
])


def parse_contents(contents, filename, date):
    content_type, content_string = contents.split(',')

    decoded = base64.b64decode(content_string)
    try:
        if 'csv' in filename:
            message = "YAAAAAAAY"
            # Assume that the user uploaded a CSV file
            dfa = pd.read_csv(
                 io.StringIO(decoded.decode('utf-8')))
        elif 'xls' in filename:
            message = "YAAAAAAAY" 
            # Assume that the user uploaded an excel file
            dfa = pd.read_excel(io.BytesIO(decoded))
        elif '.Ifc'.lower() in filename.lower():
            ifc_file = ifc.file.from_string(contents)
            #test = ifc_file.schema
            #instances = ifc_file.by_type("IfcBuildingElement")
            project = ifc_file.by_type("IfcProject")[0].Name

    except Exception as e:
        print(e)
        return html.Div([
            'There was an error processing this file.'
        ])

    return html.Div([
        html.H5(str(type(ifc_file)))
    ])


@app.callback(Output('output-data-upload', 'children'),
              Input('upload-data', 'contents'),
              State('upload-data', 'filename'),
              State('upload-data', 'last_modified'),prevent_initial_call=True)
def update_output(list_of_contents, list_of_names, list_of_dates):
    if list_of_contents is not None:
        children = [
            parse_contents(c, n, d) for c, n, d in
            zip(list_of_contents, list_of_names, list_of_dates)]
        return children



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

如果我把“项目”变量去掉,一切正常……我做错了什么?

标签: pythonplotly-dash

解决方案


推荐阅读