Skip to content

Commit

Permalink
Embed plot thumbnails in report through extras.html
Browse files Browse the repository at this point in the history
  • Loading branch information
Karan Desai committed Jun 17, 2016
1 parent 2b497a1 commit 18e2f37
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
10 changes: 4 additions & 6 deletions tardis/tests/tests_slow/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,15 @@ def pytest_runtest_makereport(item, call):

if report.when == "call":
if "plot_object" in item.fixturenames:
report.extra = item.funcargs["plot_object"].get_extras()
plot_obj = item.funcargs["plot_object"]
plot_obj.upload()
report.extra = plot_obj.get_extras()


@pytest.fixture(scope="function")
def plot_object(request):
plot_obj = PlotUploader()
plot_obj = PlotUploader(request)
setattr(request.node, "plot_obj", plot_obj)

def fin():
plot_obj.upload(request)
request.addfinalizer(fin)
return plot_obj


Expand Down
29 changes: 18 additions & 11 deletions tardis/tests/tests_slow/plot_helpers.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
import tempfile

from pytest_html import extras
import tardis


class PlotUploader(object):
def __init__(self):
def __init__(self, request):
self.request = request
self._plots = list()
self.plot_links = list()
self.plot_html = list()
self.dokuwiki_url = self.request.config.dokureport.dokuwiki_url

def add(self, plot, name):
"""
Accept a `matplotlib.pyplot.figure` and add it to self._plots.
"""
self._plots.append((plot, name))

def upload(self, request):
def upload(self):
"""
Upload the content in self._plots to dokuwiki.
"""
dokuwiki_url = request.config.dokureport.dokuwiki_url
for plot, name in self._plots:
plot_file = tempfile.NamedTemporaryFile(suffix=".png")
plot.savefig(plot_file.name)

request.config.dokureport.doku_conn.medias.add(
self.request.config.dokureport.doku_conn.medias.add(
"plots:{0}_{1}.png".format(tardis.__githash__[0:7], name),
plot_file.name
)
self.plot_links.append(extras.url(
"{0}lib/exe/fetch.php?media=plots:{1}_{2}.png".format(
dokuwiki_url, tardis.__githash__[0:7], name
), name)
)

thumbnail_html = """
<div class="image" style="float: left">
<a href="#">
<img src= "{0}lib/exe/fetch.php?media=plots:{1}_{2}.png" />
</a>
</div>
""".format(self.dokuwiki_url, tardis.__githash__[0:7], name)

self.plot_html.append(extras.html(thumbnail_html))
plot_file.close()

def get_extras(self):
return self.plot_links
return self.plot_html

0 comments on commit 18e2f37

Please sign in to comment.