Skip to content

Commit 504c292

Browse files
committed
wip
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 9fbe2b1 commit 504c292

File tree

6 files changed

+38
-65
lines changed

6 files changed

+38
-65
lines changed

plotly_static/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ fn main() -> Result<()> {
325325
} else {
326326
#[cfg(feature = "chromedriver")]
327327
{
328-
let msg = "'webdriver_download' feature disabled. Please install a '{CHROMEDRIVER_NAME}' version manually and make the environment variable 'WEBDRIVER_PATH' point to it.".to_string();
328+
let msg = format!("'webdriver_download' feature disabled. Please install a '{CHROMEDRIVER_NAME}' version manually and make the environment variable 'WEBDRIVER_PATH' point to it.");
329329
println!("cargo::warning={msg}");
330330
}
331331
#[cfg(feature = "geckodriver")]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use serde_json::Value;
1313
#[command(version)]
1414
struct Cli {
1515
/// Input file containing Plotly JSON (use '-' for stdin)
16-
#[arg(short, long, default_value = "-")]
16+
#[arg(short, long, required = true, default_value = "-")]
1717
input: String,
1818

1919
/// Output file path
File renamed without changes.

plotly_static/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ impl StaticExporter {
958958
}
959959

960960
fn static_export(&mut self, plot: &PlotData<'_>) -> Result<String> {
961-
let data_uri = template::html_body(self.offline_mode);
961+
let data_uri = template::get_html_body(self.offline_mode);
962962
let runtime = self.runtime.clone();
963963
runtime
964964
.block_on(self.extract(&data_uri, plot))

plotly_static/src/template.rs

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,27 @@ pub(crate) const PDF_EXPORT_JS_SCRIPT: &str = r##"
5353
callback('ERROR:html2pdf library not loaded');
5454
return;
5555
}
56-
56+
5757
console.log('html2pdf library is available');
5858
5959
let tempDiv = null;
6060
let mmWidth, mmHeight;
6161
6262
Plotly.newPlot(graph_div, plot).then(function() {
6363
console.log('Plotly plot created successfully');
64-
64+
6565
return Plotly.toImage(graph_div, {
6666
format: format,
6767
width: width,
6868
height: height,
6969
});
7070
}).then(function(dataUrl) {
7171
console.log('Plotly image generated successfully');
72-
72+
7373
// Convert px to mm: 1px = 0.264583 mm
7474
mmWidth = width * 0.264583;
7575
mmHeight = height * 0.264583;
76-
76+
7777
console.log('PDF dimensions (mm):', mmWidth, 'x', mmHeight);
7878
7979
// Create a temporary div for the image
@@ -112,22 +112,22 @@ pub(crate) const PDF_EXPORT_JS_SCRIPT: &str = r##"
112112
});
113113
}).then(function() {
114114
console.log('Starting PDF generation...');
115-
115+
116116
// Generate PDF with more robust configuration
117117
return html2pdf().from(tempDiv).set({
118118
margin: 0,
119119
filename: 'plotly-plot.pdf',
120120
image: { type: 'jpeg', quality: 1 },
121-
html2canvas: {
122-
scale: 1,
123-
backgroundColor: '#fff',
121+
html2canvas: {
122+
scale: 1,
123+
backgroundColor: '#fff',
124124
useCORS: true,
125125
allowTaint: true,
126126
logging: true
127127
},
128-
jsPDF: {
129-
unit: 'mm',
130-
format: [mmWidth, mmHeight],
128+
jsPDF: {
129+
unit: 'mm',
130+
format: [mmWidth, mmHeight],
131131
orientation: mmWidth > mmHeight ? 'landscape' : 'portrait',
132132
compress: true
133133
}
@@ -149,16 +149,20 @@ pub(crate) const PDF_EXPORT_JS_SCRIPT: &str = r##"
149149
});
150150
"##;
151151

152-
pub(crate) fn html_body(offline: bool) -> String {
152+
pub(crate) fn get_html_body(offline: bool) -> String {
153153
let offline_js = offline_js_sources();
154154
let cdn_js = online_js_cdn();
155+
if offline {
156+
html_body(&offline_js)
157+
} else {
158+
html_body(&cdn_js)
159+
}
160+
}
155161

162+
pub(crate) fn html_body(js_source: &str) -> String {
156163
// HTML with embedded script
157-
let html = if offline {
158-
format!(
159-
r#"
160-
<!doctype html>
161-
164+
let html = format!(
165+
r#"
162166
<html lang="en">
163167
<style>
164168
/* Ensures the image has no extra spacing */
@@ -167,41 +171,16 @@ pub(crate) fn html_body(offline: bool) -> String {
167171
margin: 0;
168172
padding: 0;
169173
background: white;
170-
}}
174+
}}
171175
</style>
172-
<head>
173-
{offline_js}
174-
</head>
175-
<body>
176-
<div id="plotly-html-element" hidden></div>
177-
</body>
176+
<head>
177+
{js_source}
178+
</head>
179+
<body>
180+
<div id="plotly-html-element" hidden></div>
181+
</body>
178182
</html>"#
179-
)
180-
} else {
181-
format!(
182-
r#"
183-
<!doctype html>
184-
<html lang="en">
185-
<style>
186-
/* Ensures the image has no extra spacing */
187-
#plotly-img-element {{
188-
display: block;
189-
margin: 0;
190-
padding: 0;
191-
background: white;
192-
}}
193-
</style>
194-
<head>
195-
{cdn_js}
196-
</head>
197-
<body>
198-
<div>
199-
</div>
200-
<div id="plotly-html-element" hidden></div>
201-
</body>
202-
</html>"#
203-
)
204-
};
183+
);
205184

206185
#[cfg(not(test))]
207186
if log_enabled!(Debug) {
@@ -216,7 +195,6 @@ pub(crate) fn html_body(offline: bool) -> String {
216195
/// Save the html file to a temporary file
217196
#[allow(unused)]
218197
pub(crate) fn to_file(data: &str) -> Result<PathBuf> {
219-
debug!("Generate plotly html file");
220198
use std::env;
221199
// Set up the temp file with a unique filename.
222200
let mut tmp_path = env::temp_dir();
@@ -225,6 +203,8 @@ pub(crate) fn to_file(data: &str) -> Result<PathBuf> {
225203
plot_name = format!("plotly_{plot_name}");
226204
tmp_path.push(plot_name);
227205

206+
debug!("Write template plotly html file to {tmp_path:?}");
207+
228208
// Save the rendered plot to the temp file.
229209
let temp_path = tmp_path
230210
.to_str()

plotly_static/src/webdriver.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ use std::process::{Child, Command, Stdio};
1414
use std::sync::{Arc, Mutex};
1515
use std::thread;
1616
#[cfg(test)]
17-
use std::{println as info, println as error, println as debug, println as warn};
17+
use std::{println as info, println as error, println as debug, println as warn, println as trace};
1818

1919
use anyhow::{anyhow, Result};
2020
#[cfg(not(test))]
21-
use log::{debug, error, info, warn};
21+
use log::{debug, error, info, trace, warn};
2222

2323
/// Environment variable for specifying the WebDriver binary path
2424
const WEBDRIVER_PATH_ENV: &str = "WEBDRIVER_PATH";
@@ -153,7 +153,6 @@ impl WebDriver {
153153

154154
let mut wd = Self::new(port)?;
155155
wd.spawn_webdriver()?;
156-
// Verify that the WebDriver is now running
157156
if Self::is_webdriver_running(port) {
158157
info!("Successfully created and started WebDriver on port {port}");
159158
Ok(wd)
@@ -175,25 +174,19 @@ impl WebDriver {
175174

176175
info!("Spawning {WEBDRIVER_BIN} on port {port} with path: {driver_path:?}");
177176

178-
// Early return if WebDriver is already running
179177
if Self::is_webdriver_running(port) {
180178
warn!("WebDriver already running on port {port}, attempting to connect instead");
181179
return Ok(());
182180
}
183181

184-
// Validate prerequisites
185182
self.validate_spawn_prerequisites()?;
186183

187-
// Spawn the process
188184
let mut child = self.spawn_process(&driver_path, port)?;
189185

190-
// Set up output monitoring
191186
self.setup_output_monitoring(&mut child, port);
192187

193-
// Store the child process
194188
self.store_child_process(child);
195189

196-
// Wait for WebDriver to become ready
197190
self.wait_for_ready(port)
198191
}
199192

@@ -323,7 +316,7 @@ impl WebDriver {
323316
info!("Starting stderr monitoring for WebDriver on port {port_for_logging}");
324317
let stderr_lines = BufReader::new(stderr).lines();
325318
for line in stderr_lines.map_while(Result::ok) {
326-
debug!("WebDriver[{port_for_logging}] stderr: {line}");
319+
trace!("WebDriver[{port_for_logging}] stderr: {line}");
327320
}
328321
info!("Stderr monitoring ended for WebDriver on port {port_for_logging}");
329322
});
@@ -336,7 +329,7 @@ impl WebDriver {
336329
info!("Starting stdout monitoring for WebDriver on port {port_for_logging}");
337330
let stdout_lines = BufReader::new(stdout).lines();
338331
for line in stdout_lines.map_while(Result::ok) {
339-
debug!("WebDriver[{port_for_logging}] stdout: {line}");
332+
trace!("WebDriver[{port_for_logging}] stdout: {line}");
340333
}
341334
info!("Stdout monitoring ended for WebDriver on port {port_for_logging}");
342335
});

0 commit comments

Comments
 (0)