def test_invalid_figure_json(): # Do image export bad_fig = {"foo": "bar"} with pytest.raises(ValueError) as err: pio.to_image(bad_fig, format="png") assert "Invalid" in str(err.value) with pytest.raises(ValueError) as err: pio.to_image(bad_fig, format="png", validate=False) assert "The image request was rejected by the orca conversion utility" in str( err.value) assert "400: invalid or malformed request syntax" in str(err.value)
def test_png_renderer_mimetype(fig1): pio.renderers.default = "png" # Configure renderer so that we can use the same parameters # to build expected image below pio.renderers["png"].width = 400 pio.renderers["png"].height = 500 pio.renderers["png"].scale = 1 image_bytes = pio.to_image(fig1, width=400, height=500, scale=1) image_str = base64.b64encode(image_bytes).decode("utf8") expected = {"image/png": image_str} pio.renderers.render_on_display = False with mock.patch("IPython.display.display") as mock_display: fig1._ipython_display_() # assert fig1._repr_mimebundle_(None, None) is None mock_display.assert_not_called() pio.renderers.render_on_display = True with mock.patch("IPython.display.display") as mock_display: fig1._ipython_display_() mock_display.assert_called_once_with(expected, raw=True)
def test_problematic_environment_variables(fig1, format): pio.orca.config.restore_defaults(reset_server=True) os.environ["NODE_OPTIONS"] = "--max-old-space-size=4096" os.environ["ELECTRON_RUN_AS_NODE"] = "1" # Do image export img_bytes = pio.to_image(fig1, format=format, width=700, height=500) assert_image_bytes(img_bytes, "fig1." + format) # Check that environment variables were restored assert os.environ["NODE_OPTIONS"] == "--max-old-space-size=4096" assert os.environ["ELECTRON_RUN_AS_NODE"] == "1"
def to_mimebundle(self, fig_dict): image_bytes = to_image( fig_dict, format=self.format, width=self.width, height=self.height, scale=self.scale, validate=False, ) if self.b64_encode: image_str = base64.b64encode(image_bytes).decode("utf8") else: image_str = image_bytes.decode("utf8") return {self.mime_type: image_str}
def test_mimetype_combination(fig1): pio.renderers.default = "png+jupyterlab" # Configure renderer so that we can use the same parameters # to build expected image below pio.renderers["png"].width = 400 pio.renderers["png"].height = 500 pio.renderers["png"].scale = 1 # pdf image_bytes = pio.to_image(fig1, format="png", width=400, height=500, scale=1) image_str = base64.b64encode(image_bytes).decode("utf8") # plotly mimetype plotly_mimetype_dict = json.loads(pio.to_json(fig1, remove_uids=False)) plotly_mimetype_dict["config"] = { "plotlyServerURL": _get_jconfig()["plotlyServerURL"] } # Build expected bundle expected = {"image/png": image_str, plotly_mimetype: plotly_mimetype_dict} pio.renderers.render_on_display = False with mock.patch("IPython.display.display") as mock_display: fig1._ipython_display_() # assert fig1._repr_mimebundle_(None, None) is None mock_display.assert_not_called() pio.renderers.render_on_display = True with mock.patch("IPython.display.display") as mock_display: fig1._ipython_display_() mock_display.assert_called_once_with(expected, raw=True)
def test_external_server_url(): # Build server url port = find_open_port() server_url = "http://{hostname}:{port}".format(hostname="localhost", port=port) # Build external orca command orca_path = which("orca") cmd_list = [orca_path] + [ "serve", "-p", str(port), "--plotly", pio.orca.config.plotlyjs, "--graph-only", ] # Run orca as subprocess to simulate external orca server DEVNULL = open(os.devnull, "wb") with orca_env(): proc = subprocess.Popen(cmd_list, stdout=DEVNULL) # Start plotly managed orca server so we can ensure it gets shut down properly pio.orca.config.port = port pio.orca.ensure_server() assert pio.orca.status.state == "running" # Configure orca to use external server pio.orca.config.server_url = server_url # Make sure that the locally managed orca server has been shutdown and the local # config options have been cleared assert pio.orca.status.state == "unvalidated" assert pio.orca.config.port is None fig = go.Figure() img_bytes = pio.to_image(fig, format="svg") assert img_bytes.startswith(b"<svg class") # Kill server orca process proc.terminate()
def test_pdf_renderer_show_override(fig1): pio.renderers.default = None # Configure renderer so that we can use the same parameters # to build expected image below pio.renderers["png"].width = 400 pio.renderers["png"].height = 500 pio.renderers["png"].scale = 1 image_bytes_png = pio.to_image(fig1, format="png", width=400, height=500, scale=1) image_str_png = base64.b64encode(image_bytes_png).decode("utf8") with mock.patch("IPython.display.display") as mock_display: pio.show(fig1, renderer="png") expected_bundle = {"image/png": image_str_png} mock_display.assert_called_once_with(expected_bundle, raw=True)
def test_latex_fig_to_image(latexfig, format): img_bytes = pio.to_image(latexfig, format=format, width=700, height=500) assert_image_bytes(img_bytes, "latexfig." + format)
def test_topojson_fig_to_image(topofig, format): img_bytes = pio.to_image(topofig, format=format, width=700, height=500) assert_image_bytes(img_bytes, "topofig." + format)
def test_to_image_default(fig1, format): pio.orca.config.default_format = format img_bytes = pio.to_image(fig1, width=700, height=500) assert_image_bytes(img_bytes, "fig1." + format)
def test_simple_to_image(fig1, format): img_bytes = pio.to_image(fig1, format=format, width=700, height=500) assert_image_bytes(img_bytes, "fig1." + format)