11module Plotly
22using HTTPClient. HTTPC
33using JSON
4- using Debug
54
65include (" plot.jl" )
6+ include (" utils.jl" )
77
88type CurrentPlot
99 filename:: String
@@ -15,16 +15,6 @@ default_options = {"filename"=>"Plot from Julia API",
1515" world_readable" => true ,
1616" layout" => {" " => " " }}
1717
18- type PlotlyCredentials
19- username:: String
20- api_key:: String
21- end
22-
23- type PlotlyConfig
24- plotly_domain:: String
25- plotly_api_domain:: String
26- end
27-
2818# # Taken from https://github.com/johnmyleswhite/Vega.jl/blob/master/src/Vega.jl#L51
2919# Open a URL in a browser
3020function openurl (url:: String )
@@ -38,172 +28,6 @@ default_opts = {
3828" platform" => " Julia" ,
3929" version" => " 0.2" }
4030
41- default_endpoints = {
42- " base" => " https://plot.ly" ,
43- " api" => " https://api.plot.ly/v2" }
44-
45- function signin (username:: String , api_key:: String , endpoints= None)
46- # Define session credentials/endpoint configuration, where endpoint is a Dict
47-
48- if endpoints != None
49- base_domain = get (endpoints, " plotly_domain" , default_endpoints[" base" ])
50- api_domain = get (endpoints, " plotly_api_domain" , default_endpoints[" api" ])
51- global plotlyconfig = PlotlyConfig (base_domain, api_domain)
52- end
53- global plotlycredentials = PlotlyCredentials (username, api_key)
54- end
55-
56- function get_credentials ()
57- # Return the session credentials if defined --> otherwise use .credentials specs
58-
59- if ! isdefined (Plotly,:plotlycredentials )
60- creds = get_credentials_file ()
61- try
62- username = creds[" username" ]
63- api_key = creds[" api_key" ]
64- global plotlycredentials = PlotlyCredentials (username, api_key)
65- catch
66- error (" Please 'signin(username, api_key)' before proceeding. See
67- http://plot.ly/API for help!" )
68- end
69- end
70-
71- # will persist for the remainder of the session
72- return plotlycredentials
73- end
74-
75- function get_config ()
76- # Return the session configuration if defined --> otherwise use .config specs
77-
78- if ! isdefined (Plotly,:plotlyconfig )
79- config = get_config_file ()
80- base_domain = get (config, " plotly_domain" , default_endpoints[" base" ])
81- api_domain = get (config, " plotly_api_domain" , default_endpoints[" api" ])
82- global plotlyconfig = PlotlyConfig (base_domain, api_domain)
83- end
84-
85- # will persist for the remainder of the session
86- return plotlyconfig
87- end
88-
89- function set_credentials_file (input_creds:: Dict )
90- # Save Plotly endpoint configuration as JSON key-value pairs in
91- # userhome/.plotly/.credentials. This includes username and api_key.
92-
93- prev_creds = {}
94-
95- try
96- prev_creds = get_credentials_file ()
97- end
98-
99- # plotly credentials file
100- userhome = get (ENV , " HOME" , " " )
101- plotly_credentials_folder = joinpath (userhome, " .plotly" )
102- plotly_credentials_file = joinpath (plotly_credentials_folder, " .credentials" )
103-
104- # check to see if dir/file exists --> if not create it
105- try
106- mkdir (plotly_credentials_folder)
107- catch err
108- isa (err, SystemError) || rethrow (err)
109- end
110-
111- # merge input creds with prev creds
112- if prev_creds != {}
113- creds = merge (prev_creds, input_creds)
114- else
115- creds = input_creds
116- end
117-
118- # write the json strings to the cred file
119- creds_file = open (plotly_credentials_file, " w" )
120- write (creds_file, JSON. json (creds))
121- close (creds_file)
122- end
123-
124- function set_config_file (input_config:: Dict )
125- # Save Plotly endpoint configuration as JSON key-value pairs in
126- # userhome/.plotly/.config. This includes the plotly_domain, and plotly_api_domain.
127-
128- prev_config = {}
129-
130- try
131- prev_config = get_config_file ()
132- end
133-
134- # plotly configuration file
135- userhome = get (ENV , " HOME" , " " )
136- plotly_config_folder = joinpath (userhome, " .plotly" )
137- plotly_config_file = joinpath (plotly_config_folder, " .config" )
138-
139- # check to see if dir/file exists --> if not create it
140- try
141- mkdir (plotly_config_folder)
142- catch err
143- isa (err, SystemError) || rethrow (err)
144- end
145-
146- # merge input config with prev config
147- if prev_config != {}
148- config = merge (prev_config, input_config)
149- else
150- config = input_config
151- end
152-
153- # write the json strings to the config file
154- config_file = open (plotly_config_file, " w" )
155- write (config_file, JSON. json (config))
156- close (config_file)
157- end
158-
159- function get_credentials_file ()
160- # Load user credentials as a Dict
161-
162- # plotly credentials file
163- userhome = get (ENV , " HOME" , " " )
164- plotly_credentials_folder = joinpath (userhome, " .plotly" )
165- plotly_credentials_file = joinpath (plotly_credentials_folder, " .credentials" )
166-
167- if ! isfile (plotly_credentials_file)
168- error (" No credentials file found. Please Set up your credentials
169- file by running set_credentials_file({\" username\" : \" your_plotly_username\" , ...
170- \" api_key\" : \" your_plotly_api_key\" })" )
171- end
172-
173- creds_file = open (plotly_credentials_file)
174- creds = JSON. parse (creds_file)
175-
176- if creds == nothing
177- creds = {}
178- end
179-
180- return creds
181- end
182-
183- function get_config_file ()
184- # Load endpoint configuration as a Dict
185-
186- # plotly configuration file
187- userhome = get (ENV , " HOME" , " " )
188- plotly_config_folder = joinpath (userhome, " .plotly" )
189- plotly_config_file = joinpath (plotly_config_folder, " .config" )
190-
191- if ! isfile (plotly_config_file)
192- error (" No configuration file found. Please Set up your configuration
193- file by running set_config_file({\" plotly_domain\" : \" your_plotly_domain\" , ...
194- \" plotly_api_domain\" : \" your_plotly_api_domain\" })" )
195- end
196-
197- config_file = open (plotly_config_file)
198- config = JSON. parse (config_file)
199-
200- if config == nothing
201- config = {}
202- end
203-
204- return config
205- end
206-
20731function get_plot_endpoint ()
20832 config = get_config ()
20933 plot_endpoint = " clientresp"
0 commit comments