Skip to content

Commit d01e636

Browse files
authored
fix plot.py to work with kaleido v1 (#418)
1 parent 7c5b7c1 commit d01e636

20 files changed

+571
-13
lines changed

nanoplotter/plot.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
from io import BytesIO
44
from urllib.parse import quote as urlquote
55
import sys
6-
from kaleido.scopes.plotly import PlotlyScope
76
import logging
87

8+
# bring in kaleido and ensure Chrome is installed
9+
import kaleido
10+
# this will download a small headless Chrome build the first time you run it
11+
kaleido.get_chrome_sync()
12+
13+
from kaleido import write_fig_sync
14+
915

1016
class Plot(object):
1117
"""A Plot object is defined by a path to the output file and the title of the plot."""
@@ -27,20 +33,18 @@ def encode(self):
2733
return self.encode1()
2834

2935
def encode1(self):
30-
"""Return the base64 encoding of the figure file and insert in html image tag."""
3136
data_uri = b64encode(open(self.path, "rb").read()).decode("utf-8").replace("\n", "")
3237
return '<img src="data:image/png;base64,{0}">'.format(data_uri)
3338

3439
def encode2(self):
35-
"""Return the base64 encoding of the fig attribute and insert in html image tag."""
3640
buf = BytesIO()
3741
self.fig.savefig(buf, format="png", bbox_inches="tight", dpi=100)
3842
buf.seek(0)
3943
string = b64encode(buf.read())
4044
return '<img src="data:image/png;base64,{0}">'.format(urlquote(string))
4145

4246
def save(self, settings):
43-
if not(self.only_report):
47+
if not self.only_report:
4448
if self.html:
4549
with open(self.path, "w") as html_out:
4650
html_out.write(self.html)
@@ -52,12 +56,10 @@ def save(self, settings):
5256
p = os.path.splitext(self.path)[0] + ".png"
5357
if os.path.exists(p):
5458
os.remove(p)
55-
5659
logging.warning("No static plots are saved due to some kaleido problem:")
5760
logging.warning(e)
5861

5962
elif self.fig:
60-
# if settings["format"] is a list, save the figure in all formats
6163
if isinstance(settings["format"], list):
6264
for fmt in settings["format"]:
6365
self.fig.savefig(
@@ -66,7 +68,11 @@ def save(self, settings):
6668
bbox_inches="tight",
6769
)
6870
else:
69-
self.fig.savefig(fname=self.path, format=settings["format"], bbox_inches="tight")
71+
self.fig.savefig(
72+
fname=self.path,
73+
format=settings["format"],
74+
bbox_inches="tight",
75+
)
7076
else:
7177
sys.exit("No method to save plot object: no html or fig defined.")
7278

@@ -77,9 +83,14 @@ def show(self):
7783
sys.stderr.write(".show not implemented for Plot instance without fig attribute!")
7884

7985
def save_static(self, figformat):
80-
scope = PlotlyScope()
81-
with open(self.path.replace("html", figformat), "wb") as f:
82-
f.write(scope.transform(self.fig, format=figformat))
83-
logging.info(
84-
f"Saved {self.path.replace('.html', '')} as {figformat} (or png for --legacy)"
85-
)
86+
"""
87+
Export a Plotly figure via Kaleido v1’s write_fig_sync.
88+
"""
89+
output_path = self.path.replace(".html", f".{figformat}")
90+
try:
91+
write_fig_sync(self.fig, path=output_path)
92+
logging.info(f"Saved {output_path} as {figformat}")
93+
except Exception as e:
94+
logging.warning("No static plots are saved due to some kaleido problem:")
95+
logging.warning(e)
96+

scripts/agm_test.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#! /bin/bash
2+
3+
#SBATCH --time=04-00:00:00
4+
#SBATCH --partition=defq
5+
6+
#SBATCH --mail-type=BEGIN,END,FAIL
7+
#SBATCH --ntasks-per-node=64
8+
#SBATCH --mem=128GB
9+
#SBATCH --nodes=1
10+
#SBATCH --job-name=nplot
11+
#SBATCH --comment=nplot
12+
13+
source /path/to/nanoplot_env/bin/activate
14+
15+
# test fresh nanoplot with kaleido update
16+
NanoPlot --fastq /path/to/test_file.fastq.gz --verbose --minqual 4 --color red -o scripts/agm_tests

scripts/agm_tests/LengthvsQualityScatterPlot_dot.html

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.
48.7 KB
Loading

scripts/agm_tests/LengthvsQualityScatterPlot_kde.html

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.
86.3 KB
Loading

0 commit comments

Comments
 (0)