Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 3661b70

Browse files
byronbyronz
authored andcommitted
✅ migrate upload file types
1 parent 99560ea commit 3661b70

File tree

6 files changed

+142
-0
lines changed

6 files changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import io
2+
import base64
3+
import os
4+
import time
5+
import pytest
6+
import pandas as pd
7+
8+
import dash
9+
from dash.dependencies import Input, Output
10+
11+
import dash_core_components as dcc
12+
import dash_html_components as html
13+
from dash_table import DataTable
14+
15+
16+
pre_style = {"whiteSpace": "pre-wrap", "wordBreak": "break-all"}
17+
18+
19+
def load_table(filetype, payload):
20+
df = (
21+
pd.read_csv(io.StringIO(base64.b64decode(payload).decode("utf-8")))
22+
if filetype == 'csv'
23+
else pd.read_excel(io.BytesIO(base64.b64decode(payload)))
24+
)
25+
26+
return html.Div(
27+
[
28+
DataTable(
29+
data=df.to_dict("records"),
30+
columns=[{"id": i} for i in ["city", "country"]],
31+
),
32+
html.Hr(),
33+
html.Div("Raw Content"),
34+
html.Pre(payload, style=pre_style),
35+
]
36+
)
37+
38+
39+
def load_data_by_type(filetype, contents):
40+
children = []
41+
_type, payload = contents.split(",")
42+
if filetype in {"csv", "xlsx", "xls"}:
43+
return load_table(filetype, payload)
44+
elif filetype in {"png", "svg"}:
45+
children = [html.Img(src=contents)]
46+
47+
children.extend(
48+
(html.Hr(), html.Div("Raw Content"), html.Pre(payload, style=pre_style))
49+
)
50+
return html.Div(children)
51+
52+
53+
@pytest.mark.parametrize("filetype", ("csv", "xlsx", "xls", "png", "svg"))
54+
def test_upft001_test_upload_with_different_file_types(filetype, dash_duo):
55+
56+
filepath = os.path.join(
57+
os.path.dirname(__file__),
58+
"upload-assets",
59+
"upft001.{}".format(filetype),
60+
)
61+
62+
app = dash.Dash(__name__)
63+
64+
app.layout = html.Div(
65+
[
66+
html.Div(filepath, id="waitfor"),
67+
html.Div(
68+
id="upload-div",
69+
children=dcc.Upload(
70+
id="upload",
71+
children=html.Div(
72+
["Drag and Drop or ", html.A("Select a File")]
73+
),
74+
style={
75+
"width": "100%",
76+
"height": "60px",
77+
"lineHeight": "60px",
78+
"borderWidth": "1px",
79+
"borderStyle": "dashed",
80+
"borderRadius": "5px",
81+
"textAlign": "center",
82+
},
83+
),
84+
),
85+
html.Div(id="output"),
86+
html.Div(DataTable(data=[{}]), style={"display": "none"}),
87+
]
88+
)
89+
90+
@app.callback(Output("output", "children"), [Input("upload", "contents")])
91+
def update_output(contents):
92+
if contents is not None:
93+
return load_data_by_type(filetype, contents)
94+
95+
dash_duo.start_server(app)
96+
97+
upload_div = dash_duo.wait_for_element("#upload-div input[type=file]")
98+
upload_div.send_keys(filepath)
99+
time.sleep(0.5)
100+
dash_duo.percy_snapshot(filepath)
101+
102+
103+
# def test_upload_gallery(self):
104+
# app = dash.Dash(__name__)
105+
# app.layout = html.Div([
106+
# html.Div(id='waitfor'),
107+
# html.Label('Empty'),
108+
# dcc.Upload(),
109+
110+
# html.Label('Button'),
111+
# dcc.Upload(html.Button('Upload File')),
112+
113+
# html.Label('Text'),
114+
# dcc.Upload('Upload File'),
115+
116+
# html.Label('Link'),
117+
# dcc.Upload(html.A('Upload File')),
118+
119+
# html.Label('Style'),
120+
# dcc.Upload([
121+
# 'Drag and Drop or ',
122+
# html.A('Select a File')
123+
# ], style={
124+
# 'width': '100%',
125+
# 'height': '60px',
126+
# 'lineHeight': '60px',
127+
# 'borderWidth': '1px',
128+
# 'borderStyle': 'dashed',
129+
# 'borderRadius': '5px',
130+
# 'textAlign': 'center'
131+
# })
132+
# ])
133+
# dash_duo.startServer(app)
134+
135+
# try:
136+
# dash_duo.wait_for_element_by_css_selector('#waitfor')
137+
# except Exception as e:
138+
# print(dash_duo.wait_for_element_by_css_selector(
139+
# '#_dash-app-content').get_attribute('innerHTML'))
140+
# raise e
141+
142+
# dash_duo.snapshot('test_upload_gallery')

0 commit comments

Comments
 (0)