Skip to content

Commit

Permalink
Merge pull request #144 from plotly/helper_functions
Browse files Browse the repository at this point in the history
Helper functions
  • Loading branch information
waralex authored Nov 10, 2021
2 parents 55ee550 + cc7ebaf commit a11022c
Show file tree
Hide file tree
Showing 9 changed files with 674 additions and 2 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Chris Parmer <chris@plotly.com>", "Alexandr Romanenko <waralex@gmail
version = "1.0.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
DashBase = "03207cf0-e2b3-4b91-9ca8-690cf0fb507e"
DashCoreComponents = "1b08a953-4be3-4667-9a23-9da06441d987"
Expand Down
1 change: 1 addition & 0 deletions src/Dash.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ include("resources/application.jl")
include("handlers.jl")
include("server.jl")
include("init.jl")
include("components_utils/_components_utils.jl")
include("plotly_base.jl")

@doc """
Expand Down
2 changes: 2 additions & 0 deletions src/components_utils/_components_utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include("express.jl")
include("table_format.jl")
102 changes: 102 additions & 0 deletions src/components_utils/express.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import Base64
export dcc_send_file, dcc_send_string, dcc_send_bytes
"""
dbc_send_file(path::AbstractString, filename = nothing; type = nothing)
Convert a file into the format expected by the Download component.
# Arguments
- `path` - path to the file to be sent
- `filename` - name of the file, if not provided the original filename is used
- `type` - type of the file (optional, passed to Blob in the javascript layer)
"""
function dcc_send_file(path, filename = nothing; type = nothing)
filename = isnothing(filename) ? basename(path) : filename
return dcc_send_bytes(read(path), filename, type = type)
end

"""
dcc_send_bytes(src::AbstractVector{UInt8}, filename; type = nothing)
dcc_send_bytes(writer::Function, data, filename; type = nothing)
Convert vector of bytes into the format expected by the Download component.
`writer` function must have signature `(io::IO, data)`
# Examples
Sending binary content
```julia
file_data = read("path/to/file")
callback!(app, Output("download", "data"), Input("download-btn", "n_clicks"), prevent_initial_call = true) do n_clicks
return dcc_send_bytes(file_data, "filename.fl")
end
```
Sending `DataFrame` in `Arrow` format
```julia
using DataFrames, Arrow
...
df = DataFrame(...)
callback!(app, Output("download", "data"), Input("download-btn", "n_clicks"), prevent_initial_call = true) do n_clicks
return dcc_send_bytes(Arrow.write, df, "df.arr")
end
```
"""
function dcc_send_bytes(src::AbstractVector{UInt8}, filename; type = nothing)

return Dict(
:content => Base64.base64encode(src),
:filename => filename,
:type => type,
:base64 => true
)
end

function dcc_send_bytes(writer::Function, data, filename; type = nothing)
io = IOBuffer()
writer(io, data)
return dcc_send_bytes(take!(io), filename, type = type)
end

"""
dcc_send_data(src::AbstractString, filename; type = nothing)
dcc_send_data(writer::Function, data, filename; type = nothing)
Convert string into the format expected by the Download component.
`writer` function must have signature `(io::IO, data)`
# Examples
Sending string content
```julia
text_data = "this is the test"
callback!(app, Output("download", "data"), Input("download-btn", "n_clicks"), prevent_initial_call = true) do n_clicks
return dcc_send_string(text_data, "text.txt")
end
```
Sending `DataFrame` in `CSV` format
```julia
using DataFrames, CSV
...
df = DataFrame(...)
callback!(app, Output("download", "data"), Input("download-btn", "n_clicks"), prevent_initial_call = true) do n_clicks
return dcc_send_string(CSV.write, df, "df.csv")
end
```
"""
function dcc_send_string(src::AbstractString, filename; type = nothing)

return Dict(
:content => src,
:filename => filename,
:type => type,
:base64 => false
)
end

function dcc_send_string(writer::Function, data, filename; type = nothing)
io = IOBuffer()
writer(io, data)
return dcc_send_string(String(take!(io)), filename, type = type)
end
Loading

0 comments on commit a11022c

Please sign in to comment.