started to play around with plotly

This commit is contained in:
didier amyot committed 2017-11-15 22:11:02 -05:00
commit 06f77aea35
10 files changed
+715

No files matched your search

+201
View File
@@ -0,0 +1,201 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## plotly implementation"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"(require '[clojupyter.misc.display :as display]\n",
" '[clojure.java.io :as io]\n",
" '[cheshire.core :as cheshire]\n",
" '[clj-http.client :as client]\n",
" '[clojure.repl :refer :all]) ;; to have all the repl goodies"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"your credentials are:\n",
" {:api_key 6FaFG4sgtpLnDrvzvFT1, :username didier, :stream_ids [8rcf9eam9j zupv6k3u4k]}\n"
]
}
],
"source": [
"(defn get-plotly-credentials\n",
" \"Returs the plotly credentials in $HOME/.plotly/.credentials\"\n",
" []\n",
" (let [home (-> \"HOME\" System/getenv io/as-file)\n",
" credential-file (io/file home \".plotly\" \".credentials\")]\n",
" (if (.exists credential-file)\n",
" (-> credential-file slurp (cheshire/parse-string true))\n",
" {})))\n",
"\n",
"(def plotly-cred (get-plotly-credentials))\n",
"(println \"your credentials are:\\n\" plotly-cred)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"#'user/upload-grid"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(defn upload-grid [grid cred] \n",
" (let [url \"https://api.plot.ly/v2/grids\";;\"https://api.plot.ly/v2/grids\" \"https://api.plot.ly/v2/grids\" ;;\n",
" headers {:Plotly-Client-Platform' \"curl\"}\n",
" auth \"didier:6FaFG4sgtpLnDrvzvFT1\"];;[(:username cred) (:api_key cred)]]\n",
" (println auth)\n",
" (client/post url {:basic-auth auth\n",
" :headers headers\n",
" :throw-entire-message? true\n",
" :form-params grid :content-type :json})))"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"#'user/my-grid"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(def my-grid {:data \n",
" {:cols\n",
" {\n",
" :height {:data [10 203 102 404] :order 1}\n",
" :weight {:data [112 234 321 34] :order 2}}}})\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"didier:6FaFG4sgtpLnDrvzvFT1\n",
"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)\n"
]
},
{
"ename": "class clojure.lang.ExceptionInfo",
"evalue": "",
"output_type": "error",
"traceback": [
" 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 "
]
}
],
"source": [
"(println (:body (upload-grid my-grid plotly-cred)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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} } } }'"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Lein-Clojure",
"language": "clojure",
"name": "lein-clojure"
},
"language_info": {
"file_extension": ".clj",
"mimetype": "text/x-clojure",
"name": "clojure",
"version": "1.9.0-RC1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}