Skip to content
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

Write Image as PDF with Box includes text "Loading [MathJax]/extensions/MathMenu.js" #3469

Open
vincentliuheyang opened this issue Nov 15, 2021 · 23 comments
Labels
bug something broken P2 considered for next cycle

Comments

@vincentliuheyang
Copy link

Hi,

截屏2021-11-15 下午4 34 46

I am trying to write plot into PDF format, but I cannot get rid of this box saying "Loading [MathJax]/extensions/MathMenu.js", will you be able to tell me how should I fix it?

Here is the code I am using to generate the plot.

fig = go.Figure()
    fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4, 5], y=[0.5, 0.6, 0.7, 0.8, 0.9, 0.9], name='A',
                             line=dict(width=4), marker=dict(size=15, symbol='star')))
    fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4, 5], y=[0.4, 0.4, 0.4, 0.4, 0.4, 0.4], name='B',
                             line=dict(width=4), marker=dict(size=15, symbol='circle')))
    fig.add_trace(
        go.Scatter(x=[0, 1, 2, 3, 4, 5], y=[0.51, 0.52, 0.53, 0.54, 0.55, 0.56], name='C',
                   line=dict(width=4), marker=dict(size=15, symbol='x')))

    fig.update_layout(
        yaxis_range=[0.2, 1],
        legend=dict(
            x=0,
            y=0,
            traceorder="reversed",
            title_font_family="Times New Roman",
            font=dict(
                family="Courier",
                size=16,
                color="black"
            ),
            bgcolor="LightSteelBlue",
            bordercolor="Black",
            borderwidth=2
        )
    )
    fig.write_image("1.pdf")
    fig.show()
@empet
Copy link

empet commented Nov 15, 2021

What Plotly version are you running? I checked your code with v 5.3.1 (the last one), and no such box is displayed.

@kuppu
Copy link

kuppu commented Nov 15, 2021

I am getting the same box in PDF outputs with Plotly Version 5.3.1
image

@empet
Copy link

empet commented Nov 15, 2021

I have no idea why that box is displayed. It could be your browser?!!!

@vincentliuheyang
Copy link
Author

It only outputs such box if I am trying to write a PDF, the browser does not have such box. One peculiar issue is that if I am going to write multiple PDF plot in one file, this box will only show in the first PDF image.

@nicolaskruchten
Copy link
Contributor

This is pretty unfortunate. The cause is that the PDF is being generated by a browser essentially doing a print operation. If this happens while MathJax is still loading, then this box will appear in the output. @jonmmease any thoughts about how to wait a bit longer?

@vincentliuheyang
Copy link
Author

This is pretty unfortunate. The cause is that the PDF is being generated by a browser essentially doing a print operation. If this happens while MathJax is still loading, then this box will appear in the output. @jonmmease any thoughts about how to wait a bit longer?

Just for now, my solution is writing a random plot before my official plot. Of course, I would like to see this problem resolved if possible.

@stansotn
Copy link

stansotn commented Nov 16, 2021

I am having the same issue with plotly 5.4.0 and kaleido 0.2.1.post1. I suspect the issue is with the latest kaleido.

@willfindlay
Copy link

I'm also running into this with:

plotly==5.4.0
plotly-express==0.4.1
kaleido==0.2.1

Thanks for the workaround @vincentliuheyang, it works well for me.

@jlehrer1
Copy link

Same issue with exact same versions as @willfindlay. Is there any workaround?

@tahashmi
Copy link

tahashmi commented Dec 14, 2021

Thanks to @vincentliuheyang.
@jlehrer1 Try something like this :-) :

import plotly.express as px

#garbage graph
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.show()
fig.write_image("random.pdf")

#actual graph to be replaced
fig1 = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig1.show()
fig1.write_image("random.pdf")

@jorgeboucas
Copy link

jorgeboucas commented Dec 14, 2021

this worked for me:

figure="some_figure.pdf"
fig=px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.write_image(figure, format="pdf")
time.sleep(2)

fig=make_figure(df,pa)
fig.write_image(figure, format="pdf")

plotly==5.3.1

@jlehrer1
Copy link

Thank you! I hadn't realized it was only on the first write when running a script. Interesting.

@eric-vader
Copy link

I am experiencing the same problem as well, do we know what causes the problem?

@nicolaskruchten
Copy link
Contributor

Yes, we know the basic cause, see plotly/Kaleido#122 for details.

Note that if the figure you're rendering doesn't actually use MathJax then you can prevent MathJax from being loaded at all by Kaleido with:

import plotly.io as pio   
pio.kaleido.scope.mathjax = None

Otherwise, if saving a "throwaway" PDF first helps, I would recommend that: basically save your figure twice.

@nicolaskruchten
Copy link
Contributor

Is anyone seeing this with Kaleido 0.1 ?

@eric-vader
Copy link

I have tried to manually downgrade to kaleido 0.1 and plot my graph and the error still exist.

@Willtl
Copy link

Willtl commented Jan 25, 2022

An easy way around is to save as .svg and then export it as .pdf using Inkscape.

@pgvaz
Copy link

pgvaz commented Mar 29, 2022

I have found a solution for that problem, at leas in my case it worked. Just disable the warnings on kaleido engine like this:

import plotly.io as pio

pio.full_figure_for_development(fig, warn=False)
fig.write_image("fig1.pdf",engine="kaleido")

@bgottschall
Copy link

bgottschall commented Dec 9, 2022

@pgvaz its probably pure luck that the message is not appearing in this case for you. When you look at the code of what you are calling here, the warn doesn't do anything but surpressing a plotly warning message that tells you you shouldn't use that function in production.
This function simply parses invalid stuff out from your figure and returns a new figure. Since you are not doing anything with the return value this call has no impact.

https://github.com/plotly/plotly.py/blob/49dfcb1868e87627f032731ef2ac48e37891baf4/packages/python/plotly/plotly/io/_kaleido.py

@theo-brown
Copy link

Saving a dummy figure before the desired figure doesn't work for me.
What browser does Plotly use by default for PDF saving? Could it be a browser-dependent issue?

@mjstahlberg
Copy link

Saving a dummy figure before the desired figure doesn't work for me.

I think a time delay of a second may be necessary.

@zhimin-z
Copy link

Yes, we know the basic cause, see plotly/Kaleido#122 for details.

Note that if the figure you're rendering doesn't actually use MathJax then you can prevent MathJax from being loaded at all by Kaleido with:

import plotly.io as pio   
pio.kaleido.scope.mathjax = None

Otherwise, if saving a "throwaway" PDF first helps, I would recommend that: basically save your figure twice.

Hi, I tried but it did remove the watermark, but fails to render the math any more. I need the math formula to be shown, what can I do now? @nicolaskruchten

@Fratorhe
Copy link

Fratorhe commented Feb 2, 2024

I am having the same issue, I have tried in Windows 10, WSL and Ubuntu...
I am using the most recent versions of the libraries.
An option could be to export to svg and then convert with inkscape (I tried with cairosvg and it does not render the mathjax).
Any updates or alternatives?

@gvwilson gvwilson self-assigned this Jul 11, 2024
@gvwilson gvwilson removed their assignment Aug 2, 2024
@gvwilson gvwilson added the P3 backlog label Aug 12, 2024
@gvwilson gvwilson changed the title Write Image as PDF with Box saying "Loading [MathJax]/extensions/MathMenu.js" Write Image as PDF with Box includes text "Loading [MathJax]/extensions/MathMenu.js" Aug 12, 2024
@gvwilson gvwilson added bug something broken P2 considered for next cycle and removed P3 backlog labels Aug 12, 2024
GavinHuttley added a commit to GavinHuttley/MutationMotif that referenced this issue Sep 21, 2024
[NEW] added a convenience class util.pdf_writer which writes
    once, then sleeps for 2 seconds, then writes again. Motivated
    by the solution here plotly/plotly.py#3469
    we only have to do this sleep once, during a session. So we create
    an instance of this writer and use it for all writes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken P2 considered for next cycle
Projects
None yet
Development

No branches or pull requests