【问题标题】:Dash dcc.upload component for large fileDash dcc.upload 大文件组件
【发布时间】:2023-02-10 23:42:54
【问题描述】:

我正在开发破折号应用程序。因为我有文件上传功能。文件大小足够大,最小约为 100MB 以支持我设置的 max_size=-1(无文件大小限制)。 下面是代码:

dcc.Upload(
 id="upload_dataset",
 children=html.Div(
  [
    "Drag and Drop or ",
    html.A(
      "Select File",
      style={
              "font-weight": "bold",
            },
            title="Click to select file.",
    ),
   ]
 ),
 multiple=False,
 max_size=-1,
)

上传的文件保存在服务器端。这个dcc.upload 组件具有属性contents,它使用 base64 以字符串格式保存整个数据。尽管浏览我开始知道在将数据发送到服务器之前,这个contents也是存储在网络浏览器内存中.

问题:对于小文件大小,将内容存储在网络浏览器内存中可能没问题。由于我的文件很大,因此浏览器可能会崩溃并冻结应用程序。

有什么方法可以绕过这种默认行为,我想以块或流的形式发送文件吗?

如何使用 dcc.upload 组件或任何其他方式在破折号中实现此目的?

【问题讨论】:

  • 作为开发 ML 应用程序时的常见用例,我仍在搜索但运气不佳。愿有人指出任何方向。

标签: python plotly-dash


【解决方案1】:

您可以使用dash-uploader 库。它允许您直接将数据从浏览器传输到服务器硬盘,因此您不会遇到任何文件大小问题。

这个库是维护者的一个爱好项目,所以它可能不是最有价值的库。不过,我今天对它进行了测试,它看起来足够稳定,我什至让它可以与在 AWS Lambda 上运行的 Dash 应用程序一起使用。

访问 more extensive documentation 以开始使用该库。

这是一个简短的代码示例,可帮助您开始使用本地版本。

要求.txt

用pip install -r requirements.txt安装

dash==2.8.1
dash-uploader==0.7.0a1
packaging==21.3

应用程序.py

复制文件 app.py 中的代码并运行该文件。它按原样运行。

import pprint
from pathlib import Path
import os
import uuid

import dash_uploader as du
import dash
from dash import Output, html


app = dash.Dash(__name__)

UPLOAD_FOLDER_ROOT = Path("./tmp") / "uploads"
du.configure_upload(
    app,
    str(UPLOAD_FOLDER_ROOT),
    use_upload_id=True,
)


def get_upload_component(id):
    return du.Upload(
        id=id,
        max_file_size=50,  # 50 Mb
        chunk_size=4,  # 4 MB
        filetypes=["csv", "json", "txt", "xlsx", "xls", "png"],
        upload_id=uuid.uuid1(),  # Unique session id
    )


def get_app_layout():

    return html.Div(
        [
            html.H1("Demo"),
            html.Div(
                children=[
                    get_upload_component("upload_data"),
                    html.Div(
                        id="upload_output",
                    ),
                ],
                style={  # wrapper div style
                    "textAlign": "center",
                    "width": "600px",
                    "padding": "10px",
                    "display": "inline-block",
                },
            ),
        ],
        style={
            "textAlign": "center",
        },
    )


# get_app_layout is a function
# This way we can use unique session id's as upload_id's
app.layout = get_app_layout


@du.callback(
    output=Output("upload_output", "children"),
    id="upload_data",
)
def callback_on_completion(status: du.UploadStatus):
    """Has some print statements to get you started in understanding the status
    object and how to access the file location of your uploaded file."""
    pprint.pprint(status.__dict__)
    print(f"Contents of {UPLOAD_FOLDER_ROOT}:
{os.listdir(UPLOAD_FOLDER_ROOT)}")
    upload_id_folder = Path(status.uploaded_files[0]).parent
    print(f"Current upload_id: {upload_id_folder.name}")
    print(
        f"Contents of subfolder {upload_id_folder.name}:
{os.listdir(upload_id_folder)}"
    )
    return html.Ul([html.Li(str(x)) for x in status.uploaded_files])


if __name__ == "__main__":
    app.run(debug=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 2020-09-10
    • 2022-01-23
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    相关资源
    最近更新 更多