I need an app that has a prompt and I can describe a state diagram with natural language. Then I click a button and the app will draw the state diagram with a nice graphics
Drop files here
or click to upload
import streamlit as st
import graphviz
def create_state_diagram(description):
# Create a new directed graph
dot = graphviz.Digraph()
dot.attr(rankdir='LR') # Left to right layout
# Parse the description and create states and transitions
lines = description.strip().split('\n')
states = set()
# First pass: collect all states
for line in lines:
if '->' in line:
parts = line.split('->')
states.add(parts[0].strip())
states.add(parts[1].strip().split('[')[0].strip())
# Add states to the graph
for state in states:
dot.node(state, state, shape='circle')
# Second pass: add transitions
for line in lines:
if '->' in line:
parts = line.split('->')
from_state = parts[0].strip()
# Handle labels in transitions
to_parts = parts[1].strip().split('[')
to_state = to_parts[0].strip()
if len(to_parts) > 1:
label = to_parts[1].strip(']').strip()
dot.edge(from_state, to_state, label=label)
else:
dot.edge(from_state, to_state)
return dot
st.title("State Diagram Generator")
st.write("""
Enter your state diagram description using the following format:
- One transition per line
- Use arrow (->) to indicate transitions
- Optionally add labels in square brackets
- Example:
A -> B [click]
B -> C [submit]
C -> A [reset]
""")
# Create a text area for input
description = st.text_area("Enter state diagram description:", height=200)
# Create button to generate diagram
if st.button("Generate Diagram"):
if description:
try:
# Generate the state diagram
dot = create_state_diagram(description)
# Render the diagram
st.graphviz_chart(dot)
except Exception as e:
st.error(f"Error generating diagram: {str(e)}")
else:
st.warning("Please enter a state diagram description.")
# Add example in the sidebar
st.sidebar.header("Example")
st.sidebar.code("""
Start -> Ready [initialize]
Ready -> Processing [start]
Processing -> Done [complete]
Done -> Ready [reset]
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?