Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to embed. #16

Closed
picousse opened this issue Aug 19, 2019 · 2 comments
Closed

How to embed. #16

picousse opened this issue Aug 19, 2019 · 2 comments

Comments

@picousse
Copy link

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
Copy link
Member

Zulko commented Aug 19, 2019

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
Copy link
Author

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

Awesome stuff! and fast!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants