How to display charts using graphviz library in Streamlit

In this recipe, we will learn how to display charts using the graphviz lib in Streamlit. We will take a look at a Streamlit application consisting of Graphviz charts.

Recipe Objective: How to display graphs using Graphviz library in Streamlit?

You can display graphs using "st.graphviz_chart" in Streamlit. It displays a graph using the dagre-d3 library.

 Syntax: st.graphviz_chart(figure_or_dot, use_container_width=False)
 Parameters:
   figure_or_dot -> It is the Graphlib graph object or dot string to display
   use_container_width -> If this is set as true, it sets the chart width to the column width. It takes precedence over the figure's native width value.

Code:

#importing required libraries
import streamlit as st
import graphviz as graphviz

# Creating a sample graphlib graph object
graph = graphviz.Digraph()
graph.edge('1','2')
graph.edge('1','3')
graph.edge('3','4')
graph.edge('3','5')
graph.edge('5','6')

#plotting the graph
st.graphviz_chart(graph)

To run the app, either create an appname.py file with the above code using any text editor, or if you are using a jupyter notebook, you need to download your .ipynb notebook as a Python (.py) file and run the same using the "streamlit run appname.py" command. Once you run the command, the app will automatically open in your default browser.

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.