Skip to content

Commit a11022c

Browse files
authored
Merge pull request #144 from plotly/helper_functions
Helper functions
2 parents 55ee550 + cc7ebaf commit a11022c

File tree

9 files changed

+674
-2
lines changed

9 files changed

+674
-2
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ authors = ["Chris Parmer <[email protected]>", "Alexandr Romanenko <waralex@gmail
44
version = "1.0.0"
55

66
[deps]
7+
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
78
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
89
DashBase = "03207cf0-e2b3-4b91-9ca8-690cf0fb507e"
910
DashCoreComponents = "1b08a953-4be3-4667-9a23-9da06441d987"

src/Dash.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ include("resources/application.jl")
2626
include("handlers.jl")
2727
include("server.jl")
2828
include("init.jl")
29+
include("components_utils/_components_utils.jl")
2930
include("plotly_base.jl")
3031

3132
@doc """
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include("express.jl")
2+
include("table_format.jl")

src/components_utils/express.jl

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import Base64
2+
export dcc_send_file, dcc_send_string, dcc_send_bytes
3+
"""
4+
dbc_send_file(path::AbstractString, filename = nothing; type = nothing)
5+
6+
Convert a file into the format expected by the Download component.
7+
8+
# Arguments
9+
- `path` - path to the file to be sent
10+
- `filename` - name of the file, if not provided the original filename is used
11+
- `type` - type of the file (optional, passed to Blob in the javascript layer)
12+
"""
13+
function dcc_send_file(path, filename = nothing; type = nothing)
14+
filename = isnothing(filename) ? basename(path) : filename
15+
return dcc_send_bytes(read(path), filename, type = type)
16+
end
17+
18+
"""
19+
dcc_send_bytes(src::AbstractVector{UInt8}, filename; type = nothing)
20+
dcc_send_bytes(writer::Function, data, filename; type = nothing)
21+
22+
Convert vector of bytes into the format expected by the Download component.
23+
`writer` function must have signature `(io::IO, data)`
24+
25+
# Examples
26+
27+
Sending binary content
28+
```julia
29+
file_data = read("path/to/file")
30+
callback!(app, Output("download", "data"), Input("download-btn", "n_clicks"), prevent_initial_call = true) do n_clicks
31+
return dcc_send_bytes(file_data, "filename.fl")
32+
end
33+
```
34+
35+
Sending `DataFrame` in `Arrow` format
36+
```julia
37+
using DataFrames, Arrow
38+
...
39+
df = DataFrame(...)
40+
callback!(app, Output("download", "data"), Input("download-btn", "n_clicks"), prevent_initial_call = true) do n_clicks
41+
return dcc_send_bytes(Arrow.write, df, "df.arr")
42+
end
43+
```
44+
"""
45+
function dcc_send_bytes(src::AbstractVector{UInt8}, filename; type = nothing)
46+
47+
return Dict(
48+
:content => Base64.base64encode(src),
49+
:filename => filename,
50+
:type => type,
51+
:base64 => true
52+
)
53+
end
54+
55+
function dcc_send_bytes(writer::Function, data, filename; type = nothing)
56+
io = IOBuffer()
57+
writer(io, data)
58+
return dcc_send_bytes(take!(io), filename, type = type)
59+
end
60+
61+
"""
62+
dcc_send_data(src::AbstractString, filename; type = nothing)
63+
dcc_send_data(writer::Function, data, filename; type = nothing)
64+
65+
Convert string into the format expected by the Download component.
66+
`writer` function must have signature `(io::IO, data)`
67+
68+
# Examples
69+
70+
Sending string content
71+
```julia
72+
text_data = "this is the test"
73+
callback!(app, Output("download", "data"), Input("download-btn", "n_clicks"), prevent_initial_call = true) do n_clicks
74+
return dcc_send_string(text_data, "text.txt")
75+
end
76+
```
77+
78+
Sending `DataFrame` in `CSV` format
79+
```julia
80+
using DataFrames, CSV
81+
...
82+
df = DataFrame(...)
83+
callback!(app, Output("download", "data"), Input("download-btn", "n_clicks"), prevent_initial_call = true) do n_clicks
84+
return dcc_send_string(CSV.write, df, "df.csv")
85+
end
86+
```
87+
"""
88+
function dcc_send_string(src::AbstractString, filename; type = nothing)
89+
90+
return Dict(
91+
:content => src,
92+
:filename => filename,
93+
:type => type,
94+
:base64 => false
95+
)
96+
end
97+
98+
function dcc_send_string(writer::Function, data, filename; type = nothing)
99+
io = IOBuffer()
100+
writer(io, data)
101+
return dcc_send_string(String(take!(io)), filename, type = type)
102+
end

0 commit comments

Comments
 (0)