gpt4 book ai didi

python - 无法连接到我要上传文件的 dash 应用程序的服务器错误

转载 作者:行者123 更新时间:2023-12-04 03:42:49 25 4
gpt4 key购买 nike

我正在尝试构建一个让用户上传 wav 文件的 dash 应用程序,我试过 -

import base64
import os
from urllib.parse import quote as urlquote

from flask import Flask, send_from_directory
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output


UPLOAD_DIRECTORY = "/project/app_uploaded_files"

if not os.path.exists(UPLOAD_DIRECTORY):
os.makedirs(UPLOAD_DIRECTORY)


# Normally, Dash creates its own Flask server internally. By creating our own,
# we can create a route for downloading files directly:
server = Flask(__name__)
app = dash.Dash(server=server)


@server.route("/download/<path:path>")
def download(path):
"""Serve a file from the upload directory."""
return send_from_directory(UPLOAD_DIRECTORY, path, as_attachment=True)


app.layout = html.Div(
[
html.H1("File Browser"),
html.H2("Upload"),
dcc.Upload(
id="upload-data",
children=html.Div(
["Drag and drop or click to select a file to upload."]
),
style={
"width": "100%",
"height": "60px",
"lineHeight": "60px",
"borderWidth": "1px",
"borderStyle": "dashed",
"borderRadius": "5px",
"textAlign": "center",
"margin": "10px",
},
multiple=True,
),
html.H2("File List"),
html.Ul(id="file-list"),
],
style={"max-width": "500px"},
)


def save_file(name, content):
"""Decode and store a file uploaded with Plotly Dash."""
data = content.encode("utf8").split(b";base64,")[1]
with open(os.path.join(UPLOAD_DIRECTORY, name), "wb") as fp:
fp.write(base64.decodebytes(data))


def uploaded_files():
"""List the files in the upload directory."""
files = []
for filename in os.listdir(UPLOAD_DIRECTORY):
path = os.path.join(UPLOAD_DIRECTORY, filename)
if os.path.isfile(path):
files.append(filename)
return files


def file_download_link(filename):
"""Create a Plotly Dash 'A' element that downloads a file from the app."""
location = "/download/{}".format(urlquote(filename))
return html.A(filename, href=location)


@app.callback(
Output("file-list", "children"),
[Input("upload-data", "filename"), Input("upload-data", "contents")],
)
def update_output(uploaded_filenames, uploaded_file_contents):
"""Save uploaded files and regenerate the file list."""

if uploaded_filenames is not None and uploaded_file_contents is not None:
for name, data in zip(uploaded_filenames, uploaded_file_contents):
save_file(name, data)

files = uploaded_files()
if len(files) == 0:
return [html.Li("No files yet!")]
else:
return [html.Li(file_download_link(filename)) for filename in files]


if __name__ == "__main__":
app.run_server(debug=False, port=8888)
但是,当我转到 http://127.0.0.1:888/ 时,我收到一条错误消息,说无法访问该站点
为什么会发生这种情况?我的动机是建立一个可以托管应用程序的网页,最终用户可以按运行并可以处理上传的文件

最佳答案

您的 URL 中的端口似乎有误,应该是 http://127.0.0.1:8888/ (而不是 http://127.0.0.1:888/ )。

关于python - 无法连接到我要上传文件的 dash 应用程序的服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65672574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com