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