Skip to content

Commit cb01070

Browse files
sglyonmalmaud
authored andcommitted
Add Reqeusts.post(::PlotlyJS.plot) method
1 parent ea65348 commit cb01070

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

src/Plotly.jl

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ using Reexport: @reexport
99

1010
include("utils.jl")
1111

12-
#export default_options, default_opts, get_config, get_plot_endpoint, get_credentials,get_content_endpoint,get_template
12+
#export default_kwargs, default_opts, get_config, get_plot_endpoint, get_credentials,get_content_endpoint,get_template
1313

1414
type CurrentPlot
1515
filename::ASCIIString
@@ -19,9 +19,8 @@ end
1919

2020
const api_version = "v2"
2121

22-
const default_options = Dict("filename"=>"Plot from Julia API",
23-
"world_readable"=> true,
24-
"layout"=>Dict())
22+
const default_kwargs = Dict{Symbol,Any}(:filename=>"Plot from Julia API",
23+
:world_readable=> true)
2524

2625
## Taken from https://github.com/johnmyleswhite/Vega.jl/blob/master/src/Vega.jl#L51
2726
# Open a URL in a browser
@@ -31,9 +30,9 @@ function openurl(url::ASCIIString)
3130
@linux_only run(`xdg-open $url`)
3231
end
3332

34-
const default_opts = Dict("origin" => "plot",
35-
"platform" => "Julia",
36-
"version" => "0.2")
33+
const default_opts = Dict{Symbol,Any}(:origin => "plot",
34+
:platform => "Julia",
35+
:version => "0.2")
3736

3837
get_plot_endpoint() = "$(get_config().plotly_domain)/clientresp"
3938

@@ -44,20 +43,19 @@ function get_content_endpoint(file_id::ASCIIString, owner::ASCIIString)
4443
"$api_endpoint/$detail/content"
4544
end
4645

47-
function plot(data::Array,options=Dict())
46+
function Requests.post(p::Plot; kwargs...)
4847
creds = get_credentials()
4948
endpoint = get_plot_endpoint()
50-
opt = merge(default_options,options)
49+
opt = merge(default_kwargs, Dict(:layout => p.layout.fields),
50+
Dict(kwargs))
5151

52-
r = post(endpoint,
53-
data = merge(default_opts,
54-
Dict(
55-
"un" => creds.username,
56-
"key" => creds.api_key,
57-
"args" => json(data),
58-
"kwargs" => json(opt)
59-
))
60-
)
52+
data = merge(default_opts,
53+
Dict("un" => creds.username,
54+
"key" => creds.api_key,
55+
"args" => json(p.data),
56+
"kwargs" => json(opt)))
57+
58+
r = post(endpoint, data=data)
6159
body=Requests.json(r)
6260

6361
if statuscode(r) != 200
@@ -66,7 +64,7 @@ function plot(data::Array,options=Dict())
6664
error(body["error"])
6765
else
6866
global currentplot
69-
currentplot=CurrentPlot(body["filename"],"new",body["url"])
67+
currentplot=CurrentPlot(body["filename"], "new", body["url"])
7068
body
7169
end
7270
end
@@ -75,55 +73,55 @@ function layout(layout_opts::Dict,meta_opts=Dict())
7573
creds = get_credentials()
7674
endpoint = get_plot_endpoint()
7775

78-
merge!(meta_opts,get_required_params(["filename","fileopt"],meta_opts))
76+
merge!(meta_opts, get_required_params(["filename", "fileopt"], meta_opts))
7977

8078
r = post(endpoint,
8179
data = merge(default_opts,
8280
Dict("un" => creds.username,
83-
"key" => creds.api_key,
84-
"args" => json(layout_opts),
85-
"origin" => "layout",
86-
"kwargs" => json(meta_opts))))
81+
"key" => creds.api_key,
82+
"args" => json(layout_opts),
83+
"origin" => "layout",
84+
"kwargs" => json(meta_opts))))
8785
__parseresponse(r)
8886
end
8987

9088
function style(style_opts,meta_opts=Dict())
9189
creds = get_credentials()
9290
endpoint = get_plot_endpoint()
9391

94-
merge!(meta_opts,get_required_params(["filename","fileopt"],meta_opts))
92+
merge!(meta_opts, get_required_params(["filename", "fileopt"], meta_opts))
9593

9694
r = post(endpoint,
9795
data = merge(default_opts,
9896
Dict("un" => creds.username,
99-
"key" => creds.api_key,
100-
"args" => json([style_opts]),
101-
"origin" => "style",
102-
"kwargs" => json(meta_opts))))
97+
"key" => creds.api_key,
98+
"args" => json([style_opts]),
99+
"origin" => "style",
100+
"kwargs" => json(meta_opts))))
103101
__parseresponse(r)
104102
end
105103

106104

107105
function getFile(file_id::ASCIIString, owner=None)
108-
creds = get_credentials()
109-
username = creds.username
110-
api_key = creds.api_key
106+
creds = get_credentials()
107+
username = creds.username
108+
api_key = creds.api_key
111109

112-
if (owner == None)
113-
owner = username
114-
end
110+
if (owner == None)
111+
owner = username
112+
end
115113

116-
endpoint = get_content_endpoint(file_id, owner)
117-
lib_version = string(default_opts["platform"], " ", default_opts["version"])
114+
endpoint = get_content_endpoint(file_id, owner)
115+
lib_version = string(default_opts["platform"], " ", default_opts["version"])
118116

119-
auth = string("Basic ", base64("$username:$api_key"))
117+
auth = string("Basic ", base64("$username:$api_key"))
120118

121-
options = Dict("Authorization"=> auth,"Plotly-Client-Platform"=> lib_version)
119+
options = Dict("Authorization"=>auth, "Plotly-Client-Platform"=>lib_version)
122120

123-
r = get(endpoint, headers=options)
124-
print(r)
121+
r = get(endpoint, headers=options)
122+
print(r)
125123

126-
__parseresponse(r)
124+
__parseresponse(r)
127125

128126
end
129127

@@ -138,7 +136,10 @@ function get_required_params(required,opts)
138136
elseif isdefined(Plotly,:currentplot)
139137
result[p] = getfield(currentplot,symbol(p))
140138
else
141-
error("Missing required param ",p, ". Make sure to create a plot first. Please refer to http://plot.ly/api, or ask [email protected]")
139+
msg = string("Missing required param $(p). ",
140+
"Make sure to create a plot first. ",
141+
" Please refer to http://plot.ly/api")
142+
error(msg)
142143
end
143144
end
144145
result

0 commit comments

Comments
 (0)