1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
mod show_iterator;
#[cfg(feature = "show_nalgebra")]
mod show_nalgebra;
#[cfg(feature = "show_ndarray")]
mod show_ndarray;
#[cfg(feature = "show_image")]
mod show_image;
use anyhow::Error;
use std::path::Path;
use std::path::PathBuf;
use std::time::SystemTime;
pub struct ContentInfo {
pub mime_type: String,
pub content: String,
}
pub trait Showable {
fn to_content_info(&self) -> Result<ContentInfo, Error>;
fn evcxr_display(&self) {
let ci = self
.to_content_info()
.expect("to be convertible into ContentInfo");
show_text_in_jupyter(ci.mime_type, ci.content);
}
fn to_html_page(&self) -> Result<String, Error> {
let dod = self.to_content_info()?;
let content = CONTENT_EMBED_HTML_TEMPLATE.replace("{{ content }}", &dod.content);
Ok(content.into())
}
fn to_html_file(&self) -> Result<PathBuf, Error> {
let mut dir = std::env::temp_dir();
dir.push(env!("CARGO_PKG_NAME"));
std::fs::create_dir_all(&dir)?;
let epoch = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
let path = dir.join(format!("show-{}.html", epoch.as_nanos()));
let html = self.to_html_page()?;
std::fs::write(&path, html)?;
Ok(path)
}
fn show_in_browser(&self) -> Result<(), Error> {
let path = self.to_html_file()?;
opener::open(path.as_os_str())?;
Ok(())
}
fn show(&self) -> Result<(), Error> {
match select_output() {
Medium::Browser => self.show_in_browser(),
Medium::Jupyter => {
self.evcxr_display();
Ok(())
}
_ => Ok(()),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Medium {
Noop,
Auto,
Jupyter,
Browser,
}
pub static OUTPUT: Medium = Medium::Auto;
fn select_output() -> Medium {
use std::env;
match OUTPUT {
Medium::Auto if env::var("SHOWATA_MEDIUM").is_ok() => {
match env::var("SHOWATA_MEDIUM").unwrap().to_lowercase().as_ref() {
"jupyter" => Medium::Jupyter,
"browser" => Medium::Browser,
_ => Medium::Noop,
}
}
Medium::Auto if env::var("EVCXR_IS_RUNTIME").is_ok() => Medium::Jupyter,
Medium::Auto => Medium::Browser,
ref m => m.clone(),
}
}
const CONTENT_EMBED_HTML_TEMPLATE: &str = r#"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div id="vis">
{{ content }}
</div>
</body>
</html>
"#;
pub fn show_text_in_jupyter<M: AsRef<str>, S: AsRef<str>>(mime_type: M, text: S) {
println!(
"EVCXR_BEGIN_CONTENT {}\n{}\nEVCXR_END_CONTENT",
mime_type.as_ref(),
text.as_ref()
);
}
pub fn show_bytes_in_jupyter<S: AsRef<str>>(mime_type: S, buffer: &[u8]) {
show_text_in_jupyter(mime_type, base64::encode(buffer))
}
pub fn show_file_in_jupyter<P: AsRef<Path>, S: AsRef<str>>(
path: P,
mime_type: S,
as_text: bool,
) -> Result<(), std::io::Error> {
let buffer = std::fs::read(path)?;
if as_text {
let text = String::from_utf8_lossy(&buffer);
show_text_in_jupyter(mime_type, &text);
} else {
show_bytes_in_jupyter(mime_type, &buffer);
}
Ok(())
}