Sympy Plot to Streamlit

Hi everyone,
I know this is mostly a sympy question, but I will give it a shot. I want to create beam solver in Streamlit, but I had a problem with Sympy. It draws the figure, but I can’t give that figure back to Streamlit.

Do you have any idea how can I do it?

See the example: Beam (Docstrings) — SymPy 1.8 documentation

It’s hacky, but you can do this:

import streamlit as st

from sympy.physics.continuum_mechanics.beam import Beam
from sympy import symbols

def beam_show(p):
  p.show() # gross hack to build the sympy plot, throws warning to console
  return st.pyplot(p._backend.fig)

# copied example from docs
R1, R2 = symbols("R1, R2")
E, I = symbols("E, I")
b = Beam(50, 20, 30)
b.apply_load(10, 2, -1)
b.apply_load(R1, 10, -1)
b.apply_load(R2, 30, -1)
b.apply_load(90, 5, 0, 23)
b.apply_load(10, 30, 1, 50)
b.apply_support(50, "pin")
b.apply_support(0, "fixed")
b.apply_support(20, "roller")
p = b.draw()

beam_show(p)

Best,
Randy

2 Likes