Edinburgh-Genome-Foundry / DnaFeaturesViewer

:eye: Python library to plot DNA sequence features (e.g. from Genbank files)
https://edinburgh-genome-foundry.github.io/DnaFeaturesViewer/
MIT License
598 stars 92 forks source link

How to embed. #16

Closed picousse closed 5 years ago

picousse commented 5 years ago

Hi, Cool lib. I wanted to use it in a webpage app using flask. However I'm a bit unsure how to embed this?

i've tried this:

graphic_record = BiopythonTranslator().translate_record(os.path.join(project.path, plasmid.path))
            plot = graphic_record.plot(figure_width = 5)
            output = io.BytesIO()
            FigureCanvas(plot).print_png(output)
            return Response(output.getvalue(), mimetype='image/png')

but errors out:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/dist-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/dist-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/dist-packages/flask_debugtoolbar/__init__.py", line 125, in dispatch_request
    return view_func(**req.view_args)
  File "/usr/local/lib/python3.7/dist-packages/flask_login/utils.py", line 261, in decorated_view
    return func(*args, **kwargs)
  File "/home/pcoussem/Software/InbioseDevelopment_Python/designstudio/designstudio/user/views_dependencies.py", line 41, in wrap
    return f(*args, **kwargs)
  File "/home/pcoussem/Software/InbioseDevelopment_Python/designstudio/designstudio/user/views_viewer.py", line 32, in viewPlasmids
    FigureCanvas(plot).print_png(output)
  File "/usr/local/lib/python3.7/dist-packages/matplotlib/backend_bases.py", line 1618, in __init__
    figure.set_canvas(self)
AttributeError: 'tuple' object has no attribute 'set_canvas'

any help/pointers what I should check?

Zulko commented 5 years ago

This maybe?

ax, _ = graphic_record.plot(figure_width = 5)
output = io.BytesIO()
FigureCanvas(ax.figure).print_png(output)
return Response(output.getvalue(), mimetype='image/png')

Also have a look at these two functions I wrote to turn a figure into a base64 url string which you can send back to the client (it probably would work if you just put that in argument to Response in your code)

ax, _ = graphic_record.plot(figure_width = 5)
data = matplotlib_figure_to_bitmap_base64_data(ax.figure, box_inches='tight')
return Response(data, mimetype='image/png')
picousse commented 5 years ago

Hi, Thanks a lot. I get some output! Now it is up to me for further tweaking.

Awesome stuff! and fast!