dkapur17 / streamlit-flow

Streamlit Component to quickly create Interactive Flow Diagrams using React Flow
https://stflow.streamlit.app
MIT License
123 stars 11 forks source link

Streamlit Flow Logo

Streamlit Flow

Streamlit App

Build beautiful, interactive flow diagrams: Powered by React Flow, Simplified by Streamlit.

Markdown Support in Node

πŸŽ‰ Version 1.5.0 is out now! πŸŽ‰

[!WARNING]

StreamlitFlow v1.5.0 has breaking changes. Please read on to understand how to migrate your code to the new version.

Say hello to the new and improved state management system that syncs the states between changes in the frontend and the backend. This means that you can now modify the state of the flow diagram from Python and see the changes reflected in the frontend without having to hack into the component's state.

Note: The updated state management uses a new StreamlitFlowState class to manage the state within streamlit. Instead of taking nodes and edges as arguments, the streamlit_flow function now takes a single StreamlitFlowState object. This means that code using earlier versions of this library will need to be tweaked slightly to work with the new version.

Old

from streamlit_flow import streamlit_flow
from streamlit_flow.elements import StreamlitFlowNode, StreamlitFlowEdge

nodes = [...]
edges = [...]

streamlit_flow('flow', nodes, edges)

New

from streamlit_flow import streamlit_flow
from streamlit_flow.elements import StreamlitFlowNode, StreamlitFlowEdge
from streamlit_flow.state import StreamlitFlowState

nodes = [...]
edges = [...]
state = StreamlitFlowState(nodes, edges)

streamlit_flow('flow', state)

The benefits we get from this are significant, as the StreamlitFlowState class makes sure that the states in the frontend and the backend are synced wth the latest changes from either side.

Example

from streamlit_flow import streamlit_flow
from streamlit_flow.elements import StreamlitFlowNode, StreamlitFlowEdge
from streamlit_flow.state import StreamlitFlowState
from uuid import uuid4

# Initialize the state if it doesn't exist
if 'flow_state' not in st.session_state:
    nodes = [...]
    edges = [...]
    st.session_state.flow_state = StreamlitFlowState(nodes, edges)

# Use any operation that alters the state, for example add node, and then rerun
if st.button("Add node"):
    new_node = StreamlitFlowNode(key=str(f"st-flow-node_{uuid4()}"), 
                                pos=(0, 0), 
                                data={'content': f'Node {len(st.session_state.curr_state.nodes) + 1}'}, 
                                node_type='default', 
                                source_position='right', 
                                target_position='left')
    st.session_state.curr_state.nodes.append(new_node)
    st.rerun()

# Use the state as the argument, as well as to store the return value
st.session_state.flow_state = streamlit_flow('flow', st.session_state.flow_state)

Minor Updates

Features

A demo for all these features can be found here.

Installation

pip install streamlit-flow-component

Running the example

Install the dependencies

git clone https://github.com/dkapur17/streamlit-flow.git
cd streamlit-flow
npm install --prefix streamlit_flow/frontend

Run the frontent

On the first terminal, run from the root of the repository

cd streamlit_flow/frontend
npm start

Run this Example Streamlit App

On the second terminal, run from the root of the repository

streamlit run example.py

Change log

Version 1.2.9

Version 1.0.0

Version 0.6.0