-
-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Helper functions #144
Merged
Merged
Helper functions #144
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1342076
download hepers
waralex 94ddb0a
download helpers
waralex c17d4ee
in work
waralex ed884ac
in work
waralex b72c7b8
Merge branch 'dev' of https://github.com/plotly/Dash.jl into dev
waralex 01e3d33
Merge branch 'dev' into helper_functions
waralex 9c2b5f9
table_format
waralex 8be6b2b
optimization
waralex dda4dd3
Trigger ci
waralex 925744e
change send_ functions signature
waralex 82cbd74
fix
waralex cc7ebaf
fix
waralex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
include("express.jl") | ||
include("table_format.jl") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
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(src::Function, filename; type = nothing) | ||
|
||
Convert vector of bytes into the format expected by the Download component. | ||
|
||
# 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("df.arr") do io | ||
Arrow.write(io, df) | ||
end | ||
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(src::Function, filename; type = nothing) | ||
io = IOBuffer() | ||
src(io) | ||
return dcc_send_bytes(take!(io), filename, type = type) | ||
end | ||
|
||
""" | ||
dcc_send_data(src::AbstractString, filename; type = nothing) | ||
dcc_send_data(src::Function, filename; type = nothing) | ||
|
||
Convert string into the format expected by the Download component. | ||
`src` is a string or function that takes a single argument - io and writes data to it | ||
|
||
# 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("df.csv") do io | ||
CSV.write(io, df) | ||
end | ||
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(src::Function, filename; type = nothing) | ||
io = IOBuffer() | ||
src(io) | ||
return dcc_send_string(String(take!(io)), filename, type = type) | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This form where the first arg to
dcc_send_bytes
is a function - it's a bit more cumbersome and confusing than the Python version that looks likeThe Python one is already a little unintuitive - I wish pandas would have just allowed
df.to_csv
to output a string if called with no buffer or file path - but at least you don't have to explicitly create a new function referencing anio
that you don't care about. I think if I were using this I'd prefer to just write it out myself:More characters but the same number of lines in total, and less to learn about this interface. Is there any other way we can make this cleaner? Are there other use cases where the function to be called as
src(io)
is already defined, the waydf.to_csv
is? If not I might suggest dropping thesrc::Function
form.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dcc_send_bytes(take!(io), "df.arr")
is also already possible. (dcc_send_bytes(src::AbstractVector{UInt8}, filename; type = nothing)
)The version with callback and io seemed convenient to me. However, if you think that it is confusing, then I can remove it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not make an analogue of
send_data_frame
because this would lead to a direct dependence of Dash on DataFrames and all packages of a particular serialization (CSV, Arrow, etc.) and thus would mean the need for a release every time the version of these packages changesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at 925744e i change
dcc_send_bytes(src::Function, filename; type = nothing)
todcc_send_bytes(writer::Function, data, filename; type = nothing)
so example above is now look likeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much cleaner! 🎉