Soerendip
Soerendip

Reputation: 9185

How to serve a pyvis graph with fastapi?

I wonder what is the best way to serve a pyvis network with FastAPI?

enter image description here

This iteractive graph was generated with pyvis and is stored in an html file and then displayed as an interactive tool in JupyterLab. I would like to serve it as part of a molecular graph exploration tool. I wonder what is the best way to do so?

import networkx
import obonet

G = obonet.read_obo('https://ftp.ebi.ac.uk/pub/databases/chebi/ontology/chebi_core.obo')

# Create smaller subgraph
H = nx.ego_graph(G, 'CHEBI:17710', 2)

H.nodes['CHEBI:24669']

from pyvis.network import Network

nt = Network('1500px', '1500px', notebook=True)
nt.from_nx(H)
nt.show('H.html')

Upvotes: 1

Views: 328

Answers (1)

dixon1e
dixon1e

Reputation: 146

First thought is to serve the template using the type HTMLResponse. Just return it as is. But you may need more work to render it well. So...

Second thought, make a template with a Jinja2 base and include your content in a block, so you can take care of any set up work necessary for the display.

See more at this response, where some code is already written that I would write anyway.

Very interested in your result.

Upvotes: 1

Related Questions