Add upload HTML preview of jupyter notebook as an artifact

This commit is contained in:
allegroai 2021-02-01 23:37:44 +02:00
parent ebc0733357
commit e3109c96d8

View File

@ -2,7 +2,7 @@ import os
import sys import sys
from copy import copy from copy import copy
from functools import partial from functools import partial
from tempfile import mkstemp from tempfile import mkstemp, gettempdir
import attr import attr
import logging import logging
@ -378,6 +378,18 @@ class _JupyterObserver(object):
task.upload_artifact( task.upload_artifact(
name='notebook', artifact_object=Path(local_jupyter_filename), name='notebook', artifact_object=Path(local_jupyter_filename),
preview='No preview available') preview='No preview available')
# noinspection PyBroadException
try:
from nbconvert.exporters import HTMLExporter # noqa
html, _ = HTMLExporter().from_filename(filename=local_jupyter_filename)
local_html = Path(gettempdir()) / 'notebook_{}.html'.format(task.id)
with open(local_html.as_posix(), 'wt') as f:
f.write(html)
task.upload_artifact(
name='notebook preview', artifact_object=local_html,
preview=' ', delete_after_upload=True)
except Exception:
pass
current_script_hash = hash(script_code + (current_cell or '')) current_script_hash = hash(script_code + (current_cell or ''))
if prev_script_hash and prev_script_hash == current_script_hash: if prev_script_hash and prev_script_hash == current_script_hash: