Save plot as png file while offline?

Hello Holoviz Discourse, I am using a JupyterLab installation that does not have access to the internet. I am attempting to save some panel objects and holoviews plots with Bokeh backend as .png files, but producing an HTTP connection error. I’ve tried both panel.save() and hv.save() without luck. I tested saving a pure Bokeh example and I could produce a .png file successfully from a plot with export_png. Is there a way to save a plot as a .png file with panel’s .save() method or the hv.save() method while offline?

This is the error:
MaxRetryError: HTTPConnectionPool(host=‘localhost’, port=34951): Max retries exceeded with url: /session/863328be-4c41-4222-b580-ebd034595844/window/maximize (Caused by NewConnectionError(’<urllib3.connection.HTTPConnection object at 0x2af70006cd60>: Failed to establish a new connection: [Errno 111] Connection refused’))

Can you post sample code? This works fine on my machine:

box = hv.BoxWhisker((np.random.randint(0, 10, 100), np.random.randn(100)), 'Group').sort()
hv_layout = pn.panel(box)
hv_layout.save('test.png')

I can post sample code but your example produces the same error I get when trying to save my examples. I think I updated bokeh and it resolved the HTTP max out error. But trying to save a png offline still is producing an error, and it seems related to this one and being offline. A side note is that I am able to export as html perfectly fine, but png really is preferable for my use case. Here’s the error:

I have selenium, geckodriver, and firefox installed but no access to internet. Any thoughts or ideas here? Appreciate the help

You can try increasing the timeout by changing the last line to

hv_layout.save(r'C:\Users\Public\Downloads\test.png', timeout = 60)

Same result unfortunately

Hi @veevee56,

I haven’t checked to see if I need to do anything extra to work offline, but running @riziles code in the following manner.

1 - restart jupyter lab kernel
2 - disable internet to laptop
3 - run code in jupyter lab cell; produces error
4 - reconnect internet to laptop
5 - re-run code in jupyter lab cell; works, saves file
6 - disconnect internet to laptop
7 - re-run code in jupyter lab cell offline; works, saves file, repeated multiple times no issues from there but…
8 - restart kernel, re-run cod in jupyter lab cell offline; back to failing and square one

I saw a bokeh issue from dev, it didn’t work from one dev version then worked on the next but was no actual resolve to what the issue is. Might be important to note this was on a linux fedora system utilising firefox.

One idea could try using Playwright instead of selenium. In my experience it works better in general.

Can you point me in the direction of any information on how to use Playwright instead of selenium for saving panel objects? I do not see anything.

Yes @veevee56 . I can give you a starting point. I don’t know if it will work for your situation. But its an alternative you can explore.

You will probably have to consult the Playwright documentation to figure out how to adjust it to your situation.

pip install playwright
playwright install
import holoviews as hv
import panel as pn
import numpy as np
from playwright.sync_api import sync_playwright

pn.extension()

box = hv.BoxWhisker((np.random.randint(0, 10, 100), np.random.randn(100)), 'Group').sort()
hv_layout = pn.Column(box)

port = 5006
print("launching server")
server = hv_layout.show(port=port, threaded=True, open=False)

with sync_playwright() as p:
    print("launching chromium")
    browser = p.chromium.launch(headless=False)
    page = browser.new_page()
    page.goto("http://localhost:5006")
    page.screenshot(path="screenshot.png")
    print("screenshot captured")
    browser.close()

server.stop()
python name_of_script.py

Another approach that you could try would be to render the HoloViews object to a Bokeh Figure and then use the Bokeh export_png function if you say that is working for you.

https://holoviews.org/user_guide/Plots_and_Renderers.html#saving-and-rendering

My guess is though that this is what hv.save does behind the scenes?

Did you get it working @veevee56 ?

Yes, I did get it working after your comments pointed me in the right direction - thank you! I was able to use the .get_root() function available on Panel objects to render as a bokeh figure, and then was able to successfully export the bokeh figure as a png with bokeh’s export_png. Appreciate your help!

1 Like