Skip to content

Commit cb3bd83

Browse files
committed
revert last two commits webdriver changes to see if windows passes
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 7a7b6b0 commit cb3bd83

File tree

3 files changed

+96
-127
lines changed

3 files changed

+96
-127
lines changed

plotly_static/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ use serde_json::map::Map as JsonMap;
274274
use urlencoding::encode;
275275
use webdriver::WebDriver;
276276

277-
use crate::template::{get_pdf_export_js_script, IMAGE_EXPORT_JS_SCRIPT};
277+
use crate::template::{IMAGE_EXPORT_JS_SCRIPT, PDF_EXPORT_JS_SCRIPT};
278278

279279
#[cfg(all(feature = "chromedriver", not(target_os = "windows")))]
280280
fn chrome_default_caps() -> Vec<&'static str> {
@@ -969,7 +969,7 @@ impl StaticExporter {
969969
plot.scale.into(),
970970
];
971971

972-
(get_pdf_export_js_script(), args)
972+
(PDF_EXPORT_JS_SCRIPT, args)
973973
}
974974
_ => {
975975
let args = vec![
@@ -980,11 +980,11 @@ impl StaticExporter {
980980
plot.scale.into(),
981981
];
982982

983-
(IMAGE_EXPORT_JS_SCRIPT.to_string(), args)
983+
(IMAGE_EXPORT_JS_SCRIPT, args)
984984
}
985985
};
986986

987-
let data = client.execute_async(&js_script, args).await?;
987+
let data = client.execute_async(js_script, args).await?;
988988

989989
// Don't close the client - keep it for reuse
990990
// client.close().await?;

plotly_static/src/template.rs

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,7 @@ pub(crate) const IMAGE_EXPORT_JS_SCRIPT: &str = r#"
3535
});
3636
"#;
3737

38-
pub(crate) fn get_pdf_export_js_script() -> String {
39-
let timeout_ms = if cfg!(feature = "ci_pdf_timeout") {
40-
500
41-
} else {
42-
250
43-
};
44-
45-
format!(
46-
r##"
38+
pub(crate) const PDF_EXPORT_JS_SCRIPT: &str = r##"
4739
const plot = arguments[0];
4840
const format = arguments[1];
4941
const width = arguments[2];
@@ -53,26 +45,26 @@ pub(crate) fn get_pdf_export_js_script() -> String {
5345
5446
const graph_div = document.getElementById('plotly-html-element');
5547
56-
if (typeof html2pdf === 'undefined') {{
48+
if (typeof html2pdf === 'undefined') {
5749
callback('ERROR:html2pdf library not loaded');
5850
return;
59-
}}
51+
}
6052
6153
let tempDiv = null;
6254
63-
const cleanup = () => {{
64-
if (tempDiv) {{
55+
const cleanup = () => {
56+
if (tempDiv) {
6557
document.body.removeChild(tempDiv);
66-
}}
67-
}};
58+
}
59+
};
6860
69-
Plotly.newPlot(graph_div, plot).then(function() {{
70-
return Plotly.toImage(graph_div, {{
61+
Plotly.newPlot(graph_div, plot).then(function() {
62+
return Plotly.toImage(graph_div, {
7163
format: format,
7264
width: width,
7365
height: height,
74-
}});
75-
}}).then(function(dataUrl) {{
66+
});
67+
}).then(function(dataUrl) {
7668
tempDiv = document.createElement('div');
7769
tempDiv.style.width = width + 'px';
7870
tempDiv.style.height = height + 'px';
@@ -96,22 +88,22 @@ pub(crate) fn get_pdf_export_js_script() -> String {
9688
img.style.boxSizing = 'border-box';
9789
tempDiv.appendChild(img);
9890
99-
return new Promise(function(resolve) {{
100-
img.onload = function() {{
91+
return new Promise(function(resolve) {
92+
img.onload = function() {
10193
// Brief delay to ensure image is fully rendered
102-
setTimeout(resolve, {timeout_ms});
103-
}};
104-
img.onerror = function() {{
94+
setTimeout(resolve, 650);
95+
};
96+
img.onerror = function() {
10597
cleanup();
10698
callback('ERROR:Failed to load image');
107-
}};
108-
}});
109-
}}).then(function() {{
110-
return html2pdf().from(tempDiv).set({{
99+
};
100+
});
101+
}).then(function() {
102+
return html2pdf().from(tempDiv).set({
111103
margin: 0,
112104
filename: 'plotly-plot.pdf',
113-
image: {{ type: 'jpeg', quality: 1}},
114-
html2canvas: {{
105+
image: { type: 'jpeg', quality: 1},
106+
html2canvas: {
115107
scale: scale,
116108
backgroundColor: '#fff',
117109
useCORS: true,
@@ -124,24 +116,22 @@ pub(crate) fn get_pdf_export_js_script() -> String {
124116
foreignObjectRendering: false,
125117
scrollY: 0,
126118
scrollX: 0
127-
}},
128-
jsPDF: {{
119+
},
120+
jsPDF: {
129121
unit: 'px',
130122
format: [width, height],
131123
orientation: width > height ? 'landscape' : 'portrait',
132124
compress: true
133-
}}
134-
}}).toPdf().output('datauristring');
135-
}}).then(function(dataUri) {{
125+
}
126+
}).toPdf().output('datauristring');
127+
}).then(function(dataUri) {
136128
cleanup();
137129
callback(dataUri);
138-
}}).catch(function(err) {{
130+
}).catch(function(err) {
139131
cleanup();
140132
callback('ERROR:' + err.toString());
141-
}});
142-
"##
143-
)
144-
}
133+
});
134+
"##;
145135

146136
pub(crate) fn get_html_body(offline: bool) -> String {
147137
let offline_js = offline_js_sources();

plotly_static/src/webdriver.rs

Lines changed: 62 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -208,114 +208,93 @@ impl WebDriver {
208208

209209
fn spawn_process(&self, driver_path: &Option<PathBuf>, port: u32) -> Result<Child> {
210210
let driver_path = driver_path.as_ref().unwrap(); // Safe unwrap since we validated above
211+
let mut command = Command::new(driver_path);
212+
command.arg(format!("--port={port}"));
213+
214+
// Add verbose flag only for chromedriver
215+
#[cfg(feature = "chromedriver")]
216+
command.arg("--verbose");
217+
218+
command
219+
.stdin(Stdio::piped())
220+
.stdout(Stdio::piped())
221+
.stderr(Stdio::piped());
222+
223+
info!(
224+
"Executing command: {:?} {:?}",
225+
command.get_program(),
226+
command.get_args()
227+
);
211228

212-
let mut command = Self::create_command(driver_path, port);
213-
Self::log_command(&command);
229+
#[cfg(target_os = "windows")]
230+
{
231+
use std::os::windows::process::CommandExt;
232+
// Try with CREATE_NO_WINDOW for headless operation
233+
command.creation_flags(0x08000000); // CREATE_NO_WINDOW flag
234+
}
214235

236+
// Try to spawn the process
215237
match command.spawn() {
216238
Ok(child) => Ok(child),
217239
Err(e) => {
218-
#[cfg(not(target_os = "windows"))]
219-
{
220-
Err(self.handle_spawn_error(e, &command, "standard method"))
221-
}
222240
#[cfg(target_os = "windows")]
223241
{
224242
// If CREATE_NO_WINDOW fails, try without any special flags
225243
error!("Failed to spawn with CREATE_NO_WINDOW: {e}");
226244
error!("Trying without special creation flags...");
227245

228-
let mut fallback_command = Self::standard_command(driver_path, port);
229-
Self::log_command(&fallback_command);
246+
let mut fallback_command = Command::new(driver_path);
247+
fallback_command.arg(format!("--port={port}"));
248+
249+
#[cfg(feature = "chromedriver")]
250+
fallback_command.arg("--verbose");
251+
252+
fallback_command
253+
.stdin(Stdio::piped())
254+
.stdout(Stdio::piped())
255+
.stderr(Stdio::piped());
230256

231257
match fallback_command.spawn() {
232258
Ok(child) => {
233259
info!("Successfully spawned WebDriver without special creation flags");
234260
Ok(child)
235261
}
236262
Err(fallback_e) => {
263+
error!("Failed to spawn '{WEBDRIVER_BIN}' with fallback method: {fallback_e}");
237264
error!("Original error: {e}");
238265
error!("Fallback error: {fallback_e}");
239-
Err(self.handle_spawn_error(
240-
fallback_e,
241-
&fallback_command,
242-
"fallback method",
243-
))
266+
error!(
267+
"Command was: {:?} {:?}",
268+
command.get_program(),
269+
command.get_args()
270+
);
271+
error!("Windows: Check if antivirus is blocking the process");
272+
error!("Windows: Check if the binary has proper permissions");
273+
error!(
274+
"WebDriver diagnostics after spawn failure:\n{}",
275+
self.get_diagnostics()
276+
);
277+
Err(anyhow!("Failed to spawn '{WEBDRIVER_BIN}': {}", fallback_e))
244278
}
245279
}
246280
}
247-
}
248-
}
249-
}
250-
251-
/// Creates a command with standard configuration (no Windows flags)
252-
fn standard_command(driver_path: &PathBuf, port: u32) -> Command {
253-
let mut command = Command::new(driver_path);
254-
command.arg(format!("--port={port}"));
255-
256-
// Add verbose flag only for chromedriver
257-
#[cfg(feature = "chromedriver")]
258-
command.arg("--verbose");
259-
260-
command
261-
.stdin(Stdio::piped())
262-
.stdout(Stdio::piped())
263-
.stderr(Stdio::piped());
264-
265-
command
266-
}
267-
268-
/// Creates a command with Windows-specific flags
269-
#[cfg(target_os = "windows")]
270-
fn create_command(driver_path: &PathBuf, port: u32) -> Command {
271-
use std::os::windows::process::CommandExt;
272-
273-
let mut command = Self::standard_command(driver_path, port);
274-
// Try with CREATE_NO_WINDOW for headless operation
275-
command.creation_flags(0x08000000); // CREATE_NO_WINDOW flag
276-
command
277-
}
278-
279-
#[cfg(not(target_os = "windows"))]
280-
fn create_command(driver_path: &PathBuf, port: u32) -> Command {
281-
Self::standard_command(driver_path, port)
282-
}
283-
284-
/// Logs command execution details
285-
fn log_command(command: &Command) {
286-
info!(
287-
"Executing command: {:?} {:?}",
288-
command.get_program(),
289-
command.get_args()
290-
);
291-
}
292-
293-
/// Handles spawn errors with appropriate logging and diagnostics
294-
fn handle_spawn_error(
295-
&self,
296-
e: std::io::Error,
297-
command: &Command,
298-
attempt: &str,
299-
) -> anyhow::Error {
300-
error!("Failed to spawn '{WEBDRIVER_BIN}' with {attempt}: {e}");
301-
error!(
302-
"Command was: {:?} {:?}",
303-
command.get_program(),
304-
command.get_args()
305-
);
306281

307-
#[cfg(target_os = "windows")]
308-
if attempt == "CREATE_NO_WINDOW" {
309-
error!("Windows: Check if antivirus is blocking the process");
310-
error!("Windows: Check if the binary has proper permissions");
282+
#[cfg(not(target_os = "windows"))]
283+
{
284+
error!("Failed to spawn '{WEBDRIVER_BIN}': {e}");
285+
error!(
286+
"Command was: {:?} {:?}",
287+
command.get_program(),
288+
command.get_args()
289+
);
290+
error!(
291+
"WebDriver diagnostics after spawn failure:\n{}",
292+
self.get_diagnostics()
293+
);
294+
Err(anyhow!("Failed to spawn '{WEBDRIVER_BIN}': {}", e))
295+
}
296+
}
311297
}
312-
313-
error!(
314-
"WebDriver diagnostics after spawn failure:\n{}",
315-
self.get_diagnostics()
316-
);
317-
318-
anyhow!("Failed to spawn '{WEBDRIVER_BIN}': {}", e)
319298
}
320299

321300
fn setup_output_monitoring(&self, child: &mut Child, port: u32) {

0 commit comments

Comments
 (0)