|
7 | 7 |
|
8 | 8 | import os |
9 | 9 | import pathlib |
| 10 | +from multiprocessing import Process |
10 | 11 |
|
11 | 12 | import numpy as np |
12 | 13 | import pandas as pd |
@@ -411,7 +412,7 @@ def plot_map(*args, sections=0, save=False, prefix="", |
411 | 412 | be overwritten. |
412 | 413 | width : int |
413 | 414 | Figure width in pixels. |
414 | | - height = int |
| 415 | + height : int |
415 | 416 | Figure height in pixels. |
416 | 417 | mapbox_style : str |
417 | 418 | Can optionally be included as one of the ``**kwargs`` |
@@ -838,7 +839,7 @@ def _save_plotly(figures, titles=None, prefix="", fnames=None, |
838 | 839 | be overwritten. |
839 | 840 | width : int |
840 | 841 | Figure width in pixels. |
841 | | - height = int |
| 842 | + height : int |
842 | 843 | Figure height in pixels. |
843 | 844 |
|
844 | 845 | """ |
@@ -873,15 +874,53 @@ def _save_plotly(figures, titles=None, prefix="", fnames=None, |
873 | 874 | + ".png") |
874 | 875 | else: |
875 | 876 | fname = fnames[fig_idx] |
| 877 | + |
876 | 878 | while True: |
877 | | - try: |
878 | | - figure.write_image(fname, |
879 | | - width = width, |
880 | | - height = height, |
881 | | - ) |
| 879 | + # sometimes writing a plotly image hanges for an unknown |
| 880 | + # reason. Hence, we call write_image in a process that is |
| 881 | + # automatically terminated after 180 seconds if nothing |
| 882 | + # happens. |
| 883 | + process = Process(target=_write_plotly, |
| 884 | + name="write_plotly", |
| 885 | + args=(figure,fname,width,height)) |
| 886 | + process.start() |
| 887 | + |
| 888 | + process.join(180) |
| 889 | + if process.is_alive(): |
| 890 | + process.terminate() |
| 891 | + process.join() |
| 892 | + continue |
| 893 | + break |
| 894 | + |
| 895 | + |
| 896 | +def _write_plotly(figure, fname, width, height): # pragma: no cover |
| 897 | + """Saves figure to file. |
| 898 | +
|
| 899 | + Automatically zooms out if plotly throws a ValueError when trying |
| 900 | + to zoom in too much. |
| 901 | +
|
| 902 | + Parameters |
| 903 | + ---------- |
| 904 | + figure : plotly.graph_objects.Figure |
| 905 | + Object to save. |
| 906 | + fname : string or path-like |
| 907 | + Path to save figure to. |
| 908 | + width : int |
| 909 | + Figure width in pixels. |
| 910 | + height : int |
| 911 | + Figure height in pixels. |
| 912 | +
|
| 913 | + """ |
| 914 | + |
| 915 | + while True: |
| 916 | + try: |
| 917 | + figure.write_image(fname, |
| 918 | + width = width, |
| 919 | + height = height, |
| 920 | + ) |
| 921 | + break |
| 922 | + except ValueError as error: |
| 923 | + figure.layout.mapbox.zoom -= 1 |
| 924 | + if figure.layout.mapbox.zoom < 1: |
| 925 | + print(error) |
882 | 926 | break |
883 | | - except ValueError as error: |
884 | | - figure.layout.mapbox.zoom -= 1 |
885 | | - if figure.layout.mapbox.zoom < 1: |
886 | | - print(error) |
887 | | - break |
|
0 commit comments