Files
graphly/.ipynb_checkpoints/plotly-checkpoint.ipynb
T

9.2 KiB

plotly implementation

In [29]:
(require '[clojupyter.misc.display :as display]
         '[clojure.java.io :as io]
         '[cheshire.core :as cheshire]
         '[clj-http.client :as client]
         '[clojure.repl :refer :all])  ;; to have all the repl goodies

To access plotly api, we need an api key. The standard way is to store the api key in $HOME/.plotly/.credentials in a json object as defined in https://plot.ly/python/getting-started/. For that you will need to create a plotly account.

In [61]:
(defn get-plotly-credentials
    "Returs the plotly credentials in $HOME/.plotly/.credentials"
    []
    (let [home (-> "HOME" System/getenv io/as-file)
          credential-file (io/file home ".plotly" ".credentials")]
        (if (.exists credential-file)
            (-> credential-file slurp (cheshire/parse-string true))
            {})))

(def plotly-cred  (get-plotly-credentials))
(println "your credentials are:\n" plotly-cred)
your credentials are:
 {:api_key 6FaFG4sgtpLnDrvzvFT1, :username didier, :stream_ids [8rcf9eam9j zupv6k3u4k]}

Next, we need to create "grids". Grids are dataset stored on plotly that we can then plot. The plotting is done in two phases, uploding the datasets and then plotting the datasets.

In [86]:
(defn upload-grid [grid cred]  
  (let [url  "https://api.plot.ly/v2/grids";;"https://api.plot.ly/v2/grids" "https://api.plot.ly/v2/grids" ;;
        headers {:Plotly-Client-Platform' "curl"}
        auth "didier:6FaFG4sgtpLnDrvzvFT1"];;[(:username cred) (:api_key cred)]]
    (println auth)
    (client/post url {:basic-auth auth
                      :headers headers
                      :throw-entire-message? true
                      :form-params grid :content-type :json})))
Out [86]:
#'user/upload-grid
In [87]:
(def my-grid {:data 
              {:cols
               {
                   :height {:data [10 203 102 404] :order 1}
                   :weight {:data [112 234 321 34] :order 2}}}})
    
Out [87]:
#'user/my-grid
In [87]:
(println (:body (upload-grid my-grid plotly-cred)))
didier:6FaFG4sgtpLnDrvzvFT1
ExceptionInfo clj-http: status 401 {:request-time 908, :repeatable? false, :protocol-version {:name "HTTP", :major 1, :minor 1}, :streaming? true, :chunked? true, :reason-phrase "UNAUTHORIZED", :headers {"Server" "nginx", "Via" "1.1 google", "Content-Type" "application/json", "Alt-Svc" "clear", "X-Frame-Options" "SAMEORIGIN", "Strict-Transport-Security" "max-age=31536000; includesubdomains; preload", "WWW-Authenticate" "Bearer realm=\"api\"", "Allow" "GET, POST, HEAD, OPTIONS", "Connection" "close", "Transfer-Encoding" "chunked", "Set-Cookie" ["plotly_sess_pr=z3l151u0weiecpy1jgmvp4bqaic9ksoo; Domain=.plot.ly; expires=Thu, 30-Nov-2017 03:09:02 GMT; httponly; Max-Age=1209600; Path=/; secure" "plotly_csrf_pr=b3rFF0ApmkYeHCK9Rjn9kKW30z2OyDkZ; Domain=.plot.ly; expires=Sat, 17-Nov-2018 03:09:02 GMT; Max-Age=31622400; Path=/; secure" "plotly_anoncsrf_pr=1Pv2vgnQIhIqOnw6bFy1kf4V2qAyXJ0r; Domain=.plot.ly; expires=Fri, 17-Nov-2017 03:09:02 GMT; httponly; Max-Age=86400; Path=/; secure" "GCLB=CNmkk46g4orXggE; path=/; HttpOnly; expires=Fri, 17-Nov-2017 03:09:02 GMT"], "Date" "Thu, 16 Nov 2017 03:09:02 GMT", "Vary" "Accept, Authorization, Cookie, Accept-Language", "x-xss-protection" "1; mode=block", "Content-Language" "en"}, :orig-content-encoding nil, :status 401, :length -1, :body "{\"errors\":[{\"code\":\"UNKNOWN\",\"message\":\"No Plotly-Client-Platform header found\",\"path\":null,\"field\":null}],\"detail\":\"No Plotly-Client-Platform header found\"}", :trace-redirects []}  slingshot.support/stack-trace (support.clj:201)
            support.clj:   201 slingshot.support$stack_trace/invoke                   
             client.clj:   238 clj_http.client$exceptions_response/invokeStatic       
             client.clj:   230 clj_http.client$exceptions_response/invoke             
             client.clj:   248 clj_http.client$wrap_exceptions$fn__2420/invoke        
             client.clj:   724 clj_http.client$wrap_accept$fn__2666/invoke            
             client.clj:   746 clj_http.client$wrap_accept_encoding$fn__2673/invoke   
             client.clj:   707 clj_http.client$wrap_content_type$fn__2660/invoke      
             client.clj:   948 clj_http.client$wrap_form_params$fn__2769/invoke       
             client.clj:   983 clj_http.client$wrap_nested_params$fn__2791/invoke     
             client.clj:   882 clj_http.client$wrap_method$fn__2727/invoke            
            cookies.clj:   131 clj_http.cookies$wrap_cookies$fn__773/invoke           
              links.clj:    63 clj_http.links$wrap_links$fn__2196/invoke              
             client.clj:  1009 clj_http.client$wrap_unknown_host$fn__2799/invoke      
             client.clj:  1181 clj_http.client$request_STAR_/invokeStatic             
             client.clj:  1174 clj_http.client$request_STAR_/invoke                   
             client.clj:  1199 clj_http.client$post/invokeStatic                      
             client.clj:  1195 clj_http.client$post/doInvoke                          
            RestFn.java:   423 clojure.lang.RestFn/invoke                             
               core.clj:  3206 clojure.core$eval/invokeStatic                         
               core.clj:  3202 clojure.core$eval/invoke                               
               main.clj:   243 clojure.main$repl$read_eval_print__8572$fn__8575/invoke
               main.clj:   261 clojure.main$repl$fn__8581/invoke                      
               main.clj:   177 clojure.main$repl/doInvoke                             
            RestFn.java:  1523 clojure.lang.RestFn/invoke                             
               AFn.java:   152 clojure.lang.AFn/applyToHelper                         
               AFn.java:   144 clojure.lang.AFn/applyTo                               
               core.clj:   657 clojure.core$apply/invokeStatic                        
               core.clj:  1965 clojure.core$with_bindings_STAR_/invokeStatic          
            RestFn.java:   425 clojure.lang.RestFn/invoke                             
               AFn.java:    22 clojure.lang.AFn/run                                   
ThreadPoolExecutor.java:  1149 java.util.concurrent.ThreadPoolExecutor/runWorker      
ThreadPoolExecutor.java:   624 java.util.concurrent.ThreadPoolExecutor$Worker/run     
            Thread.java:   748 java.lang.Thread/run                                   

curl --user didier:6FaFG4sgtpLnDrvzvFT1 -H "Content-Type: application/json" -H "Plotly-Client-Platform: curl" https://api.plot.ly/v2/grids --data '{"data": { "cols": { "first column": {"data": ["a", "b", "c"], "order": 0}, "second column": {"data": [1, 2, 3], "order": 1} } } }'