Skip to content

Commit fb87952

Browse files
author
Ioannis Giagkiozis
committed
jupyter evcxr kernel example
1 parent e326fd2 commit fb87952

File tree

5 files changed

+136
-1
lines changed

5 files changed

+136
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
target/
22
.idea
33
Cargo.lock
4-
gh-pages/
4+
gh-pages/
5+
Untitled.ipynb

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Annotations support (TODO add link to documentation and examples).
1212
- Docstrings.
1313
- `ndarray` support (TODO add link to documentation and examples).
14+
- Jupyter lab support (TODO add link to documentation and examples).
1415
### Changed
1516
- Removed `num` dependence.
1617
- Removed `plotly_orca` and the `orca` feature. Use the `kaleido` feature for static image generation.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 2,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
":dep plotly = { git = \"https://github.com/igiagkiozis/plotly.git\", branch = \"dev\", features = [\"kaleido\", \"ndarray\"] }\n",
10+
":dep itertools-num = \"0.1.3\""
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 3,
16+
"metadata": {},
17+
"outputs": [],
18+
"source": [
19+
"extern crate plotly;\n",
20+
"extern crate rand_distr;\n",
21+
"extern crate itertools_num;\n",
22+
"extern crate itertools;"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 4,
28+
"metadata": {},
29+
"outputs": [],
30+
"source": [
31+
"use itertools_num::linspace;\n",
32+
"use plotly::common::{\n",
33+
" ColorScale, ColorScalePalette, DashType, Fill, Font, Line, LineShape, Marker, Mode, Title,\n",
34+
"};\n",
35+
"use plotly::layout::{Axis, BarMode, Layout, Legend, TicksDirection};\n",
36+
"use plotly::{Bar, NamedColor, Plot, Rgb, Rgba, Scatter};\n",
37+
"use rand_distr::{Distribution, Normal, Uniform};"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 11,
43+
"metadata": {},
44+
"outputs": [],
45+
"source": [
46+
"fn simple_scatter_plot(show: bool) {\n",
47+
" let n: usize = 100;\n",
48+
" let t: Vec<f64> = linspace(0., 10., n).collect();\n",
49+
" let y: Vec<f64> = t.iter().map(|x| x.sin()).collect();\n",
50+
"\n",
51+
" let trace = Scatter::new(t, y).mode(Mode::Markers);\n",
52+
" let mut plot = Plot::new();\n",
53+
" plot.add_trace(trace);\n",
54+
" let layout = Layout::new().height(800);\n",
55+
" plot.set_layout(layout);\n",
56+
" plot.show_jupyter();\n",
57+
"}"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 12,
63+
"metadata": {},
64+
"outputs": [
65+
{
66+
"data": {
67+
"text/html": [
68+
"<div id=\"UEE9DFbjdI6e5d1V2pRR\" class=\"plotly-graph-div\" style=\"height:100%; width:100%;\"></div>\n",
69+
"<script type=\"text/javascript\">\n",
70+
" window.PLOTLYENV=window.PLOTLYENV || {};\n",
71+
" if (document.getElementById(\"UEE9DFbjdI6e5d1V2pRR\")) {\n",
72+
" var d3 = Plotly.d3;\n",
73+
" var image_element= d3.select('#image-export');\n",
74+
" var trace_0 = {\"type\":\"scatter\",\"mode\":\"markers\",\"x\":[0.0,0.10101010101010101,0.20202020202020202,0.30303030303030304,0.40404040404040403,0.5050505050505051,0.6060606060606061,0.7070707070707071,0.8080808080808081,0.9090909090909091,1.0101010101010102,1.1111111111111112,1.2121212121212122,1.3131313131313131,1.4141414141414141,1.5151515151515151,1.6161616161616161,1.7171717171717171,1.8181818181818181,1.9191919191919191,2.0202020202020203,2.121212121212121,2.2222222222222223,2.323232323232323,2.4242424242424243,2.525252525252525,2.6262626262626263,2.727272727272727,2.8282828282828283,2.929292929292929,3.0303030303030303,3.131313131313131,3.2323232323232323,3.3333333333333335,3.4343434343434343,3.5353535353535355,3.6363636363636362,3.7373737373737375,3.8383838383838382,3.9393939393939394,4.040404040404041,4.141414141414141,4.242424242424242,4.343434343434343,4.444444444444445,4.545454545454545,4.646464646464646,4.747474747474747,4.848484848484849,4.94949494949495,5.05050505050505,5.151515151515151,5.252525252525253,5.353535353535354,5.454545454545454,5.555555555555555,5.656565656565657,5.757575757575758,5.858585858585858,5.959595959595959,6.0606060606060606,6.161616161616162,6.262626262626262,6.363636363636363,6.4646464646464645,6.565656565656566,6.666666666666667,6.767676767676767,6.8686868686868685,6.96969696969697,7.070707070707071,7.171717171717171,7.2727272727272725,7.373737373737374,7.474747474747475,7.575757575757575,7.6767676767676765,7.777777777777778,7.878787878787879,7.979797979797979,8.080808080808081,8.181818181818182,8.282828282828282,8.383838383838384,8.484848484848484,8.585858585858587,8.686868686868687,8.787878787878787,8.88888888888889,8.98989898989899,9.09090909090909,9.191919191919192,9.292929292929292,9.393939393939394,9.494949494949495,9.595959595959595,9.696969696969697,9.797979797979798,9.8989898989899,10.0],\"x0\":null,\"dx\":null,\"y\":[0.0,0.1008384202581046,0.2006488565226854,0.2984138044476411,0.3931366121483298,0.48385164043793466,0.5696341069089657,0.6496095135057065,0.7229625614794605,0.7889454628442574,0.8468855636029834,0.8961922010299563,0.9363627251042848,0.9669876227092996,0.9877546923600838,0.9984522269003895,0.9989711717233568,0.9893062365143401,0.9695559491823237,0.9399216514301312,0.9007054462029555,0.8523071179396752,0.7952200570230491,0.7300262299764464,0.6573902466827755,0.5780525851065732,0.4928220425889235,0.40256749066949654,0.30820901749007684,0.2107085480771929,0.11106003812412972,0.010279341240534697,-0.09060614703340773,-0.19056796287548539,-0.28858705872043244,-0.38366419180611233,-0.47483011082223947,-0.5611554368152017,-0.6417601376193878,-0.7158224992291902,-0.7825875026542022,-0.8413745208608701,-0.8915842573351402,-0.9327048555318336,-0.9643171169287782,-0.9860987744909296,-0.9978277779792126,-0.9993845576124357,-0.9907532430056771,-0.9720218249588334,-0.9433812584459996,-0.9051235159501367,-0.8576386109880517,-0.8014106221689697,-0.7370127583189133,-0.6651015149788224,-0.586409981847235,-0.5017403693939113,-0.4119558308308628,-0.31797166281061867,-0.22074597455506334,-0.12126992053716677,-0.020557596287260064,0.08036429967028173,0.18046693235991093,0.27872981867755725,0.37415123057121996,0.4657584070256517,0.5526174707464059,0.6338429484489058,0.7086067976992182,0.7761468482835805,0.8357745720522589,0.8868821020290788,0.9289484292312513,0.9615447140268235,0.9843386578838236,0.9970978909438748,0.9996923408861117,0.9920955589323228,0.9743849894755358,0.9467411805833543,0.9094459434244625,0.8628794793817836,0.8075165041395626,0.7439214082568444,0.6727425035622647,0.5947054140244975,0.510605678474283,0.4213006405886069,0.32770070881349983,0.23076007532505177,0.13146698864295842,0.03083367906114098,-0.07011396040064677,-0.1703468323280965,-0.26884312591038406,-0.3645987336558887,-0.45663748763377376,-0.5440211108893698],\"y0\":null,\"dy\":null,\"meta\":null,\"custom_data\":null};\n",
75+
"var data = [trace_0];\n",
76+
"var layout = {\"height\":800};\n",
77+
" Plotly.newPlot('UEE9DFbjdI6e5d1V2pRR', data, layout, {\"responsive\": true});\n",
78+
" };\n",
79+
"</script>"
80+
],
81+
"text/plain": [
82+
"()"
83+
]
84+
},
85+
"execution_count": 12,
86+
"metadata": {},
87+
"output_type": "execute_result"
88+
}
89+
],
90+
"source": [
91+
"simple_scatter_plot(true)"
92+
]
93+
}
94+
],
95+
"metadata": {
96+
"kernelspec": {
97+
"display_name": "Rust",
98+
"language": "rust",
99+
"name": "rust"
100+
},
101+
"language_info": {
102+
"codemirror_mode": "rust",
103+
"file_extension": ".rs",
104+
"mimetype": "text/rust",
105+
"name": "Rust",
106+
"pygment_lexer": "rust",
107+
"version": ""
108+
}
109+
},
110+
"nbformat": 4,
111+
"nbformat_minor": 4
112+
}

plotly/src/plot.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ impl Plot {
256256
}
257257
}
258258

259+
pub fn initialize_notebook() {
260+
println!("EVCXR_BEGIN_CONTENT text/html\n{}\nEVCXR_END_CONTENT", r#"<script src="https://cdn.plot.ly/plotly-1.54.6.min.js" charset="utf-8"></script>"#)
261+
}
262+
259263
pub fn show_jupyter(&self) {
260264
let html = self.to_inline_html(None);
261265
println!("EVCXR_BEGIN_CONTENT text/html\n{}\nEVCXR_END_CONTENT", html);

plotly_kaleido/build.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,25 @@ use std::io;
1010
#[cfg(target_os = "linux")]
1111
const KALEIDO_URL: &str =
1212
"https://github.com/plotly/Kaleido/releases/download/v0.0.1/kaleido_linux-0.0.1.zip";
13+
1314
#[cfg(target_os = "windows")]
1415
const KALEIDO_URL: &str =
1516
"https://github.com/plotly/Kaleido/releases/download/v0.0.1/kaleido_win-0.0.1.zip";
17+
1618
#[cfg(target_os = "macos")]
1719
const KALEIDO_URL: &str =
1820
"https://github.com/plotly/Kaleido/releases/download/v0.0.1/kaleido_mac-0.0.1.zip";
1921

22+
#[cfg(target_os = "linux")]
23+
const KALEIDO_BIN: &str = "kaleido";
24+
25+
#[cfg(target_os = "windows")]
26+
const KALEIDO_BIN: &str = "kaleido.exe";
27+
28+
#[cfg(target_os = "macos")]
29+
const KALEIDO_BIN: &str = "kaleido";
30+
31+
2032
fn extract_zip(p: &PathBuf, zip_file: &PathBuf) -> Result<()> {
2133
let file = fs::File::open(&zip_file).unwrap();
2234
let mut archive = zip::ZipArchive::new(file).unwrap();
@@ -79,6 +91,11 @@ fn main() -> Result<()> {
7991
dst = dst.parent().unwrap().to_path_buf();
8092
dst = dst.join("plotly_kaleido");
8193

94+
let kaleido_binary = dst.join("kaleido").join("bin").join(KALEIDO_BIN);
95+
if kaleido_binary.exists() {
96+
return Ok(());
97+
}
98+
8299
let kaleido_zip_file = p.join("kaleido.zip");
83100

84101
let mut cmd = Command::new("cargo")

0 commit comments

Comments
 (0)