1- # __precompile__(true)
2-
31module Plotly
42
5- using URIParser
3+ using HTTP
64using Reexport
75using JSON
86@reexport using PlotlyJS
7+ using DelimitedFiles, Pkg, Base64 # stdlib
8+
99export set_credentials_file, RemotePlot, download_plot, savefig_remote, post
1010
1111const _SRC_ATTRS = let
@@ -30,15 +30,15 @@ function openurl(url::String)
3030 end
3131end
3232
33- openurl (url:: URI ) = openurl (string (url))
33+ openurl (url:: HTTP. URI ) = openurl (string (url))
3434
3535"""
3636Proxy for a plot stored on the Plotly cloud.
3737"""
3838struct RemotePlot
39- url:: URI
39+ url:: HTTP. URI
4040end
41- RemotePlot (url:: String ) = RemotePlot (URI (url))
41+ RemotePlot (url:: String ) = RemotePlot (HTTP . URI (url))
4242
4343"""
4444 fid(rp::RemotePlot)
@@ -60,36 +60,36 @@ Display a plot stored in the Plotly cloud in a browser window.
6060Base. open (p:: RemotePlot ) = openurl (p. url)
6161
6262"""
63- Post a local Plotly plot to the Plotly cloud.
63+ Post a local Plotly plot to the Plotly cloud using V2 api .
6464
65- Must be signed in first.
65+ Must be signed in first. See `Plotly.signin` for details on how to do that
6666"""
67- function post (p:: Plot ; fileopt= get_config (). fileopt, filename= nothing , kwargs... )
67+ function post_v2 (p:: Plot ; fileopt= get_config (). fileopt, filename= nothing , kwargs... )
6868 JSON. lower (p)
6969 fileopt = Symbol (fileopt)
7070 grid_fn = string (filename, " _" , " Grid" )
7171 clean_p = srcify (p; fileopt= fileopt, grid_fn= grid_fn, kwargs... )
7272 if fileopt == :overwrite
73- file_data = try_lookup (filename)
73+ file_data = try_lookup (filename) # # Api call 1
7474 if file_data == nothing
7575 fileopt = :create
7676 else
77- res = plot_update (file_data[" fid" ], figure= clean_p)
77+ res = plot_update (file_data[" fid" ], figure= clean_p) # # Api call 2
7878 return RemotePlot (res[" web_url" ])
7979 end
8080 end
8181 if fileopt == :create || fileopt == :new
8282 if filename == nothing
83- res = plot_create (clean_p; kwargs... )
83+ res = plot_create (clean_p; kwargs... ) # # Api call 2 (or 1)
8484 else
8585 parent_path = dirname (filename)
8686 if ! isempty (parent_path)
8787 res = plot_create (
8888 clean_p; parent_path= parent_path,
8989 filename= basename (filename), kwargs...
90- )
90+ ) # # Api call 2 (or 1)
9191 else
92- res = plot_create (clean_p; filename= filename, kwargs... )
92+ res = plot_create (clean_p; filename= filename, kwargs... ) # # Api call 2 (or 1)
9393 end
9494 end
9595
@@ -99,7 +99,7 @@ function post(p::Plot; fileopt=get_config().fileopt, filename=nothing, kwargs...
9999 end
100100end
101101
102- function post_v1 (p:: Plot ; kwargs... )
102+ function post (p:: Plot ; kwargs... )
103103 # call JSON.lower to apply themes
104104 JSON. lower (p)
105105 config = get_config ()
@@ -119,27 +119,29 @@ function post_v1(p::Plot; kwargs...)
119119 data = merge (
120120 default_opts,
121121 Dict (
122- " un " => creds. username,
123- " key" => creds. api_key,
124- " args" => json (p. data),
125- " kwargs" => json (opt)
122+ :un => creds. username,
123+ : key => creds. api_key,
124+ : args => JSON . json (p. data),
125+ : kwargs => JSON . json (opt)
126126 )
127127 )
128128
129- r = post (endpoint, data= data)
130- body = Requests. json (r)
131- if Requests. statuscode (r) ≠ 200
129+ res = HTTP. request (" POST" , endpoint,
130+ [" Content-Type" => " application/x-www-form-urlencoded" ],
131+ HTTP. URIs. escapeuri (data))
132+ body = JSON. parse (String (deepcopy (res. body)))
133+ if res. status ≠ 200
132134 throw (PlotlyError (" Non-sucessful status code: $(statuscode (r)) " ))
133135 elseif " error" ∈ keys (body) && body[" error" ] ≠ " "
134136 throw (PlotlyError (body[" error" ]))
135137 elseif " detail" ∈ keys (body) && body[" detail" ] ≠ " "
136138 throw (PlotlyError (body[" detail" ]))
137139 end
138- return RemotePlot (URI (body[" url" ]))
140+ return RemotePlot (HTTP . URI (body[" url" ]))
139141end
140142
141143post (p:: PlotlyJS.SyncPlot ; kwargs... ) = post (p. plot; kwargs... )
142- post_v1 (p:: PlotlyJS.SyncPlot ; kwargs... ) = post_v1 (p. plot; kwargs... )
144+ post_v2 (p:: PlotlyJS.SyncPlot ; kwargs... ) = post_v2 (p. plot; kwargs... )
143145
144146"""
145147 srcify!(p::Plot; fileopt::Symbol=:overwrite, grid_fn=nothing, kwargs...)
@@ -177,7 +179,7 @@ function extract_grid_data!(p::Plot)
177179 error (" bad key..." )
178180 end
179181 end
180- function add_to_grid! (k1:: Vector , v:: Associative )
182+ function add_to_grid! (k1:: Vector , v:: AbstractDict )
181183 for (k2, v2) in v
182184 add_to_grid! (vcat (k1, k2), v2)
183185 end
0 commit comments