question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

export_png not exporting figures correctly

See original GitHub issue

This is a continuation of #7621

export_png (and export_svgs for that matter) do not work as expected when attempting to export figures after they were added to a layout curdoc().

Here is the example code:

# -*- coding: utf-8 -*-
'''
After the layout has been added to the root, exporting the png of a fig is
not possible anymore
With "bokeh serve export_test.py", the console output for me is the following:
doc_layout works
fig works
Layout is added to root
doc_layout works
fig throws error
'''
from bokeh.io import curdoc
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.layouts import layout, widgetbox
from bokeh.io import export_png
from bokeh.models.widgets import Button


def download():
    try:
        export_png(doc_layout, filename="export doc_layout.png")
        print('doc_layout works')
    except Exception as ex:
        print('doc_layout throws error:', ex)
    try:
        export_png(fig, filename="export fig.png")
        print('fig works')
    except Exception as ex:
        print('fig throws error:', ex)


source = ColumnDataSource({'x': [2, 3, 4], 'y': [0.5, 1.5, 2.5]})
fig = figure(plot_width=1000, plot_height=300)
fig.hbar(y='y', right='x', height=0.4, source=source)

button = Button(label="Download")
button.on_click(download)
doc_layout = layout([[widgetbox(button)], [fig]], sizing_mode='fixed')

#download()  # Test 1

print('Layout is added to root')
curdoc().add_root(doc_layout)
curdoc().title = "Export PNG test"

#download()  # Test 2

I start the server with bokeh serve export_test.py. After I click the Download button, the following output is generated:

You are generating standalone HTML/JS output, but trying to use real Python
callbacks (i.e. with on_change or on_event). This combination cannot work.

Only JavaScript callbacks may be used with standalone output. For more
information on JavaScript callbacks with Bokeh, see:

    http://bokeh.pydata.org/en/latest/docs/user_guide/interaction/callbacks.html

Alternatively, to use real Python callbacks, a Bokeh server application may
be used. For more information on building and running Bokeh applications, see:

    http://bokeh.pydata.org/en/latest/docs/user_guide/server.html

doc_layout works
fig throws error: Message: {"errorMessage":"undefined is not an object (evaluating 'document.getElementsByClassName('bk-root')[0].children[0].getBoundingClientRect')","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"171","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:62171","User-Agent":"selenium/3.12.0 (python linux)"},"httpVersion":"1.1","method":"POST","post":"{\"script\": \"\\nreturn document.getElementsByClassName('bk-root')[0].children[0].getBoundingClientRect()\\n\", \"args\": [], \"sessionId\": \"5f2d8590-7620-11e8-94ca-bf888776a2b5\"}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/execute","relative":"/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/5f2d8590-7620-11e8-94ca-bf888776a2b5/execute"}}
Screenshot: available via screen

If I comment out the line download() # Test 1, the correct png is generated. This is only possible before curdoc().add_root(doc_layout). I still cannot save the png of a figure, once it has been included in a layout and added to root. Am I doing someting wrong? Does it have something to do with the new standalone HTML warning?

Software: Bokeh = 0.13.0 Tornado = 5.0.2 Selenium = 3.12.0 Pillow = 5.1.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
znstridercommented, Sep 20, 2018

Thank you AzraelDD, your solution works for me.

1reaction
bryevdvcommented, Sep 19, 2018

Closing since, as I said the original code now works for me, and also the annoying warning has been silenced. Please reopen or comment if issues are persisting with latest dev versions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Export as to export png moves the image on the canvas
Solved: I'm not sure if this is normal behavior but I have a png with transparency. It's meant to be use as a...
Read more >
Problems with export to png - Beginners' Questions - Inkscape
Background: I made the figure in Excel, saved it as a svg and edited it using inkscape (mostly adding bars and asterisks). The...
Read more >
some graphics missing when exporting artboard using "export ...
Hello, I am experiencing strange results during artboard export - after export to bitmap file, there is often some part of graphics missing....
Read more >
Export to png or jpg not creating any file despite saying file ...
Now, I installed it using ppa(ppa:inkscape.dev/stable) and it is working fine now. Able to export files properly and files appearing at export ......
Read more >
Exporting a PNG File - the Inkscape Beginners' Guide!
Often, it's useful to export your work into a raster graphics file format, to show off or use it with other programs. Open...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found