Skip to content

Commit f910307

Browse files
committed
Misc finishing touches
1 parent 3fd28b7 commit f910307

File tree

4 files changed

+33
-75
lines changed

4 files changed

+33
-75
lines changed

REQUIRE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
julia 0.4
22
Requests 0.3.5
33
JSON
4-
PlotlyJS 0.1.0
5-
Compat 0.7.12
4+
PlotlyJS 0.2.0
5+
Compat 0.7.20
66
Reexport

src/Plotly.jl

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
module Plotly
44
using Compat
5+
using Compat: String
56
using Requests
67
using JSON
78
using Reexport: @reexport
89
import Requests: URI, post
910

1011
@reexport using PlotlyJS
1112
export post
13+
export set_credentials_file
1214

1315
include("utils.jl")
1416

@@ -33,14 +35,24 @@ openurl(url::URI) = openurl(string(url))
3335

3436
get_plot_endpoint() = "$(get_config().plotly_domain)/clientresp"
3537

36-
38+
"""
39+
Proxy for a plot stored on the Plotly cloud.
40+
"""
3741
immutable RemotePlot
3842
url::URI
3943
end
4044
RemotePlot(url) = RemotePlot(URI(url))
4145

46+
"""
47+
Display a plot stored in the Plotly cloud in a browser window.
48+
"""
4249
Base.open(p::RemotePlot) = openurl(p.url)
4350

51+
"""
52+
Post a local Plotly plot to the Plotly cloud.
53+
54+
Must be signed in first.
55+
"""
4456
function post(p::Plot; kwargs...)
4557
creds = get_credentials()
4658
endpoint = get_plot_endpoint()
@@ -54,7 +66,6 @@ function post(p::Plot; kwargs...)
5466
"kwargs" => json(opt)))
5567

5668
r = post(endpoint, data=data)
57-
# body=Requests.json(r)
5869
body = parse_response(r)
5970
return RemotePlot(URI(body["url"]))
6071
end
@@ -95,7 +106,11 @@ function style(style_opts, meta_opts=Dict(); meta_kwargs...)
95106
parse_response(post(endpoint, data=data))
96107
end
97108

109+
"""
110+
Transport a plot from the Plotly cloud to a local `Plot` object.
98111
112+
Must be signed in first if the plot is not public.
113+
"""
99114
function Base.download(plot::RemotePlot)
100115
creds = get_credentials()
101116
username = creds.username
@@ -111,32 +126,12 @@ function Base.download(plot::RemotePlot)
111126
end
112127
endpoint = URI(plot.url, path="$path.json")
113128
response = get(endpoint, headers=options)
114-
return JSON.parse(Plot, bytestring(response))
115-
end
116-
117-
download_plot(url) = download(RemotePlot(url))
118-
119-
function get_required_params(required,opts)
120-
# Priority given to user-inputted opts, then currentplot
121-
result=Dict()
122-
for p in required
123-
global currentplot
124-
if haskey(opts,p)
125-
result[p] = opts[p]
126-
elseif isdefined(Plotly,:currentplot)
127-
result[p] = getfield(currentplot,symbol(p))
128-
else
129-
msg = string("Missing required param $(p). ",
130-
"Make sure to create a plot first. ",
131-
" Please refer to http://plot.ly/api")
132-
error(msg)
133-
end
134-
end
135-
result
129+
local_plot = JSON.parse(Plot, bytestring(response))
130+
return PlotlyJS.SyncPlot(local_plot)
136131
end
137132

138133
immutable PlotlyError <: Exception
139-
msg::UTF8String
134+
msg::String
140135
end
141136

142137
function Base.show(io::IO, err::PlotlyError)

src/utils.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ default_endpoints = Dict(
55
"api" => "https://api.plot.ly/v2")
66

77
type PlotlyCredentials
8-
username::ASCIIString
9-
api_key::ASCIIString
8+
username::String
9+
api_key::String
1010
end
1111

1212
type PlotlyConfig
13-
plotly_domain::ASCIIString
14-
plotly_api_domain::ASCIIString
13+
plotly_domain::String
14+
plotly_api_domain::String
1515
end
1616

17-
function signin(username::ASCIIString, api_key::ASCIIString, endpoints=None)
17+
function signin(username::String, api_key::String, endpoints=nothing)
1818
# Define session credentials/endpoint configuration, where endpoint is a Dict
1919

2020
global plotlycredentials = PlotlyCredentials(username, api_key)
2121

2222
# if endpoints are specified both the base and api domains must be specified
23-
if endpoints != None
23+
if endpoints != nothing
2424
try
2525
base_domain = endpoints["plotly_domain"]
2626
api_domain = endpoints["plotly_api_domain"]
@@ -34,7 +34,7 @@ end
3434
function get_credentials()
3535
# Return the session credentials if defined --> otherwise use .credentials specs
3636

37-
if !isdefined(Plotly,:plotlycredentials)
37+
if !isdefined(Plotly, :plotlycredentials)
3838

3939
creds = get_credentials_file()
4040

test/tests.jl

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,13 @@ using Base.Test
66

77
Plotly.signin("unittest","tfzz811f5n", Dict("plotly_domain"=>"https://plot.ly","plotly_api_domain"=>"https://api.plot.ly"))
88

9-
(x0,y0) = [1,2,3], [4,5,6]
10-
(x1,y1) = [1,2,3], [2,10,12]
11-
12-
response = Plotly.plot([[x0 y0] [x1 y1]], Dict("filename" => "basic_test_plot"))
13-
@test response["error"] == ""
14-
15-
datastyle = Dict("line"=>Dict("color"=> "rgb(84, 39, 143)", "width"=> 4))
16-
style_res = Plotly.style(datastyle, Dict("filename" => "basic_test_plot"))
17-
@test style_res["error"] == ""
18-
19-
layout_res = Plotly.layout(Dict("title"=>"Hello World"), Dict("filename" => "basic_test_plot"))
20-
@test layout_res["error"] == ""
21-
22-
# just check that these won't raise an error
23-
trace1 = Plotly.line(sin, Dict("left"=>0, "right"=>10, "step"=>1))
24-
trace2 = Plotly.histogram(cos, Dict("left"=>0, "right"=>10, "step"=>1))
25-
trace3 = Plotly.box(abs, Dict("left"=>0, "right"=>10, "step"=>1))
26-
trace4 = Plotly.scatter(log, Dict("left"=>0, "right"=>10, "step"=>1))
27-
response = Plotly.plot([trace1, trace2, trace3, trace4], Dict("filename" => "test_plot"))
28-
@test response["error"] == ""
29-
30-
# and one last check
31-
response = Plotly.plot([sin, cos, abs, log], Dict("left"=>eps(), "right"=>10, "step"=>1, "filename" => "test_plot"))
32-
@test response["error"] == ""
33-
34-
#= getFile is broken because the /content endpoint isn't supported anymore?
35-
figure = Plotly.getFile("5", "chris")
36-
@test length(figure["data"][1]["x"]) == 501
37-
@test haskey(figure["layout"], "xaxis")
38-
39-
response = Plotly.plot(figure["data"], Dict("layout"=> figure["layout"], "filename" => "test_get_figure_plot"))
40-
@test response["error"] == ""
41-
=#
9+
# Check for no error
10+
original_plot = Plotly.plot([scatter(x=[1,2],y=[3,4])])
11+
remote_plot = post(original_plot)
12+
downloaded_plot = download(remote_plot)
4213

4314
#test get_plot_endpoint
4415
endpoints = Dict("plotly_domain"=>"my_plotly_domain", "plotly_api_domain"=>"my_plotly_api_domain")
4516
Plotly.signin("test_username", "test_api_key", endpoints)
4617
plot_endpoint = Plotly.get_plot_endpoint()
4718
@test plot_endpoint == "my_plotly_domain/clientresp"
48-
49-
#test get_content_endpoint
50-
endpoints = Dict("plotly_domain"=>"my_plotly_domain", "plotly_api_domain"=>"my_plotly_api_domain")
51-
Plotly.signin("test_username", "test_api_key", endpoints)
52-
fid = "123_fake"
53-
owner = "test_owner"
54-
content_endpoint = Plotly.get_content_endpoint(fid, owner)
55-
@test content_endpoint == "my_plotly_api_domain/v2/files/test_owner:123_fake/content"

0 commit comments

Comments
 (0)