Skip to content

plotly.show() does not work on Windows 10 #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ana-jiangR opened this issue Dec 4, 2022 · 5 comments · Fixed by #131
Closed

plotly.show() does not work on Windows 10 #128

ana-jiangR opened this issue Dec 4, 2022 · 5 comments · Fixed by #131

Comments

@ana-jiangR
Copy link

By design, the API "plot.show()" would open a default web browser to display an interactive plot.
However, under my environment(Windows 10, plotly="0.8.3"), it doesn't work. The default web browser was not opened either.

I found out that there was an issue message from cmd:
The process cannot access the file because it is being used by another process

https://github.com/igiagkiozis/plotly/blob/49bd3a87fbaa3e507e9a2664f57feda89c35b95b/plotly/src/plot.rs#L465

//Line 465
    #[cfg(target_os = "windows")]
    fn show_with_default_app(temp_path: &str) {
        use std::process::Command;
        Command::new("cmd")
            .arg("/C")
            .arg(format!(r#"start {}"#, temp_path))
            .output()
            .expect(DEFAULT_HTML_APP_NOT_FOUND);
    }

After investigation, I suspect it's because, that the temp html file that should be opened was being used by another process, while this "another" process is the process itself which has not yet close the file handle in Rust program.

Could you fix this please? Thanks! :)

@mfreeborn
Copy link
Contributor

I would gratefully accept a PR with a fix - I'm short on time at the moment to fix this personally. I'm not immediately sure why it works on linux but not Windows; the implementations should be equivalent.

@juanespj
Copy link
Contributor

juanespj commented Jan 8, 2023

Hello I was playing with this function and figured it out I will send a pull request with this!!

    fn show_with_default_app(temp_path: &str) {
        use std::process::Command;
        Command::new("cmd")
            .arg(format!("/Cstart {}", temp_path))
            .spawn()
                .expect(DEFAULT_HTML_APP_NOT_FOUND);
            // .output()
            // .expect(DEFAULT_HTML_APP_NOT_FOUND);
    }```

@mfreeborn
Copy link
Contributor

Closed by #129

@GSTRonnie
Copy link

GSTRonnie commented Jan 9, 2023

I also came upon this problem and figured out that it can be resolved by closing the file in Plot::show(), for example enclosing it in a scope, before calling Plot::show_with_default_app(). E.g.:

// Save the rendered plot to the temp file.
let temp_path = temp.to_str().unwrap();
{
    let mut file = File::create(temp_path).unwrap();
    file.write_all(rendered.as_bytes())
        .expect("failed to write html output");
    file.flush().unwrap();
}

// Hand off the job of opening the browser to an OS-specific implementation.
Plot::show_with_default_app(temp_path);

@mfreeborn mfreeborn reopened this Jan 9, 2023
@mfreeborn
Copy link
Contributor

Hmm, I wonder if this is a more robust solution than #129. It's always good to release resources as early as possible for exactly this reason.

mfreeborn added a commit that referenced this issue Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants