I am trying to show LangChain
graph in my python module code. Not Jupiter notebook. However, the following code snippet:
from IPython.display import Image, display
from langgraph.graph import StateGraph, MessagesState
graph_builder = StateGraph(MessagesState)
simple_graph = graph_builder.compile()
display(Image(simple_graph.get_graph().draw_mermaid_png()))
shows:
<IPython.core.display.Image object>
instead of the graph. I am on Ubuntu.
Question: Can IPython
be used in python module code? According to https://ipython.org/, it seems only possible for the Jupyter Notebook environment. If that's the case, how to use matplotlib
or pillow
to plot the graph?
Have to save the image to a file and use pillow
to show()
it:
from PIL import Image
png_graph = simple_graph.get_graph().draw_mermaid_png()
# Save the PNG data to a file
with open("/tmp/graph.png", "wb") as f:
f.write(png_graph)
img = Image.open("/tmp/graph.png")
img.show()