Streamlit for ML #5.1 - Custom React Components in Streamlit Setup

James Briggs · Beginner ·🧠 Large Language Models ·4y ago

Key Takeaways

The video demonstrates how to use custom React components in Streamlit, including setup, installation, and configuration, as well as leveraging Streamlit's caching mechanism to improve performance. It also showcases how to embed React components in Streamlit for interactive visualizations and create a custom dashboard for machine learning models.

Full Transcript

today we're going to have a look at how we can build custom components in a streamlit using react and external libraries like material ui so there are already plenty of components that you can use directly from streamlit and also from the streaming community but obviously you may find yourself needing to use something that doesn't already exist so we'll go through that process uh fortunately it's not too difficult it does require a little bit of like front-end code but streaming it doesn't make it pretty straightforward so let's have a quick look at what this card might look like or this component is a card so all we have for our custom component is this little card in the middle here the rest of this is just streamlit and then this bit here is based on a material ui component so if i go back we just have a title subtitle text or body of text and a link here which is a material ui button click on it and for now it's just taken to google but we can essentially [Music] like an object in python we can pass whatever values we like to this component so here are the actual cards from material ui that we're basing this on and with these cards we can include much more than what i have shown you this is just the absolute basics like we can include these little profile images drop downs options and so on even though expanders pictures so yeah there's a lot we can do with it and as far as i know there isn't anything like this uh available directly within the stream so let's go ahead and set up our environment for developing this component so if you are on mac and you have homebrew installed you can just go through install node and this will install node.js and the node package manager i already have it installed so i'm not going to rerun it if you are not on mac or you don't have homebrew you can download it from the node.js website so over here this here node.js so old um and then download okay you have windows mac and so on so once you have that installed you also need to install extremely i'm going to assume you already have it installed but if not you just pip and sold streamlit now i have streamla in another environment so i'm going to activate that on that streamlit and the streaming side of things i will run from this terminal window now to build our component for streamlit we need to follow a certain structure and unfortunately streamlit provides us with a few templates that we can start from to make our lives a little bit easier so let me show you where you can find those so if we just type in um streamlit and github or maybe we can go with streamlit components template it's probably easier we come here okay and we have the streamlit component template uh repo we just want to git clone this so we click on code over here copy this and then switch back to our terminal um navigate to whatever folder you're going to store the template folder within so i'm going to go to documents and projects and i'm just going to write git clone and then i'm going to clone the that the component template repository okay um let's open that nvs code and we'll have a look at what we have in here okay so on the right over here we have our directory structure we have a few different templates so we're going to go this template here which uses react and within this template directory this is our actual like project so we can almost ignore the rest of this stuff here just anything within template is what we care about uh we have set up pi so we're going to use this and manifest for creating a pip package which will contain our component so to actually use that component we install our component and then we just import it into our like our streamlit app script or app.pi usually and then use it and [Music] within here we just have the default file names here or default directory names of my component we're going to change some of these uh inside here we have init pi so that's where we're initializing our streaming server or app from um we'll go through that it's fine we have this front end which is anything in here is the react side of things so if we go a little further we have some styling you know the index and then in here we have these uh tsx files okay so in here we're using typescript so if you know typescript is really good if not it's not too much different from python i mean it's fairly different but it's not unbelievably different so i think if you know python probably read this at least and kind of follow what we're doing um but i also don't really know typescript i'd like to just get through uh putting something simple together with this so most of the work we're going to do is going to be my component tsx but we are going to modify a lot of these files as well so first let's rename everything uh because everything at the moment is using default names um and we obviously i'll build a custom component we want to give our own name um that makes it a little more identifiable so we'll start by going to my component up here i'm going to rename that to st card component so st just streamline card component down here we have my component going to rename that to card component basically anywhere that we have my components we modify to card component we update these imports okay so inside card component here if we just find and replace so find my component and replace that with card component place all of those and see if we have anything for my component nope okay save that and let's have a look at the index i think we also have something in here um just just my component here okay replace those so we have everything in there and in set of pi we also want to update the name here so this will change it to streamlight card component okay so this defines it for our for our package later on and it's actually sorry so this should align with the directory order folder that we have here so not streamlit but st and in our manifest here we also need to update this to be um st card component so this needs to point to the front end so that's building out the build directory yeah but we will so that needs to go st card component in here okay um so that should pretty much be everything we need to rename maybe other little bits are actually in init here we also want to change this to sd card component so here sd card component it's anything else okay now that's so let's go ahead and actually uh initialize the current or the basically default rather than the things we've renamed version of this component and see how that looks so we'll go to and here we have the component template and inside there's a template um see yeah template and then we have st card component so in here we also have front end directory and inside here we have all of our packages and everything so like the node side of stuff so first first thing you need to do is actually install the node packages we need for our component so to start we just do npm install and this is just going to install everything from this package.json file so run that it might take a moment so just give it a second okay so that's done and next thing we want to do is install the node packages are required specifically for the card that we're going to build so these are all the node packages we just installed there for streamlit to function like the core of the packages that we need but because we are using this material ui card thing we need a few extra things so we need to npm install again this is kind of similar to like a pip install um although it's specific to this directory okay so when we pip install um install them to our python environment in this case it's almost like the environment is this directory and this like um this project correct project so npm install we need mui material and ui icons material icons material emotion [Music] react and also emotion styled now i think this is probably going to give us an error let's see okay so yes we get this error it's um we have a dependency conflict with i think if i remember correctly yeah so we have to stream it component template and this um it throws some dependency conflicts with the mui material stuff so it's annoying but we can just get around it by adding this legacy peer depths now i haven't i've been playing around this i haven't noticed any issues uh pop up from using this live spear depths but i'm obviously you just need to be aware like don't just throw it in there all the time but in this case it seems to work fine okay so with that we can we should be able to run um everything so let's go ahead and let's go ahead and do that the first thing we want to do is so we're going to run two things here we're going to run within this front end directory we write npm start this is going to initialize or execute the server that hosts our react component and then we also need to open another terminal window we also need to navigate to our template directory so cd documents projects it is a com component template templates okay cd template and in here we have a sd card component again we have this init pi init pi is like well it's not exactly that uh but while so in development it is just like a [Music] template app for just running and testing um our component so we just write streamlit run init.hi so previous videos you've probably seen me private streaming run app.pi this is kind of acting as our app.pi whilst we're in the development stage um but once we switch to um a release version this init pi uh we will modify a little bit and it will not be for for doing this streamline run it will do something slightly different it will extract everything from a from a compiled build distribution so now we have these we're hosting two two servers we have this localhost three zero zero one if we if we go over there open that we're going to see well nothing so this is just hosting our react component about our react component by itself doesn't actually show anything so we actually need to go over and open the streamlit um localhost so that is in eight five zero one okay and now we have this um like template component so we can click here we it updates this basically okay maybe even change this enter yeah we get this okay so that's cool um but obviously we want to build a custom component um and we'll go ahead and we'll do that in the in the next video uh for now i think we'll leave it there we've sort of set up the environment we've started running the default component and i think that's good enough for now and yeah in the in the next video we'll be able to really focus on actually building the component itself um which means we're going to be toying with the code over in the card component tsx uh quite a bit and cleaning this up and yeah creating our card so i hope that's been useful thank you very much for watching and i will see you in the next one

Original Description

▶️ Streamlit for ML Part 5.2: https://www.youtube.com/watch?v=mxm8ihWoVbk&list=PLIUOU7oqGTLg5ssYxPGWaci6695wtosGw&index=6 There are plenty of prebuilt components designed by Streamlit themselves, and if you can't find what you need, there are even community-built components. If you're still stuck, and there is just no component that covers what you need, we can build our own custom components. To do this we do need to start playing with the lower-level web technologies that Streamlit itself is built upon. So it isn't as simple as using a prebuilt component. However, thanks to pre-made templates, it isn't too hard to create a new component. In this sub-series, we'll learn exactly how to create custom components. We'll focus on designing an interactive card component using Material UI design elements. ▶️ Streamlit for ML Playlist: https://www.youtube.com/watch?v=JLKUV-LiXjk&list=PLIUOU7oqGTLg5ssYxPGWaci6695wtosGw&index=1 📕 Article: Coming soon 🤖 70% Discount on the NLP With Transformers in Python course: https://bit.ly/3DFvvY5 🎉 Subscribe for Article and Video Updates! https://jamescalam.medium.com/subscribe https://medium.com/@jamescalam/membership 📖 Friend link to article: Coming soon 👾 Discord: https://discord.gg/c5QtDB9RAP 00:00 Intro 02:19 Environment Setup 03:42 Starting with a Template 07:41 Naming for Card Component 11:31 Installing Node Packages 15:12 Running the Component
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from James Briggs · James Briggs · 0 of 60

← Previous Next →
1 Stoic Philosophy Text Generation with TensorFlow
Stoic Philosophy Text Generation with TensorFlow
James Briggs
2 How to Build TensorFlow Pipelines with tf.data.Dataset
How to Build TensorFlow Pipelines with tf.data.Dataset
James Briggs
3 Every New Feature in Python 3.10.0a2
Every New Feature in Python 3.10.0a2
James Briggs
4 How-to Build a Transformer for Language Classification in TensorFlow
How-to Build a Transformer for Language Classification in TensorFlow
James Briggs
5 How-to use the Kaggle API in Python
How-to use the Kaggle API in Python
James Briggs
6 Language Generation with OpenAI's GPT-2 in Python
Language Generation with OpenAI's GPT-2 in Python
James Briggs
7 Text Summarization with Google AI's T5 in Python
Text Summarization with Google AI's T5 in Python
James Briggs
8 How-to do Sentiment Analysis with Flair in Python
How-to do Sentiment Analysis with Flair in Python
James Briggs
9 Python Environment Setup for Machine Learning
Python Environment Setup for Machine Learning
James Briggs
10 Sequential Model - TensorFlow Essentials #1
Sequential Model - TensorFlow Essentials #1
James Briggs
11 Functional API - TensorFlow Essentials #2
Functional API - TensorFlow Essentials #2
James Briggs
12 Training Parameters - TensorFlow Essentials #3
Training Parameters - TensorFlow Essentials #3
James Briggs
13 Input Data Pipelines - TensorFlow Essentials #4
Input Data Pipelines - TensorFlow Essentials #4
James Briggs
14 6 of Python's Newest and Best Features (3.7-3.9)
6 of Python's Newest and Best Features (3.7-3.9)
James Briggs
15 Novice to Advanced RegEx in Less-than 30 Minutes + Python
Novice to Advanced RegEx in Less-than 30 Minutes + Python
James Briggs
16 Building a PlotLy $GME Chart in Python
Building a PlotLy $GME Chart in Python
James Briggs
17 How-to Use The Reddit API in Python
How-to Use The Reddit API in Python
James Briggs
18 How to Build Custom Q&A Transformer Models in Python
How to Build Custom Q&A Transformer Models in Python
James Briggs
19 How to Build Q&A Models in Python (Transformers)
How to Build Q&A Models in Python (Transformers)
James Briggs
20 How-to Decode Outputs From NLP Models (Python)
How-to Decode Outputs From NLP Models (Python)
James Briggs
21 Identify Stocks on Reddit with SpaCy (NER in Python)
Identify Stocks on Reddit with SpaCy (NER in Python)
James Briggs
22 Sentiment Analysis on ANY Length of Text With Transformers (Python)
Sentiment Analysis on ANY Length of Text With Transformers (Python)
James Briggs
23 Unicode Normalization for NLP in Python
Unicode Normalization for NLP in Python
James Briggs
24 The NEW Match-Case Statement in Python 3.10
The NEW Match-Case Statement in Python 3.10
James Briggs
25 Multi-Class Language Classification With BERT in TensorFlow
Multi-Class Language Classification With BERT in TensorFlow
James Briggs
26 How to Build Python Packages for Pip
How to Build Python Packages for Pip
James Briggs
27 How-to Structure a Q&A ML App
How-to Structure a Q&A ML App
James Briggs
28 How to Index Q&A Data With Haystack and Elasticsearch
How to Index Q&A Data With Haystack and Elasticsearch
James Briggs
29 Q&A Document Retrieval With DPR
Q&A Document Retrieval With DPR
James Briggs
30 How to Use Type Annotations in Python
How to Use Type Annotations in Python
James Briggs
31 Extractive Q&A With Haystack and FastAPI in Python
Extractive Q&A With Haystack and FastAPI in Python
James Briggs
32 Sentence Similarity With Sentence-Transformers in Python
Sentence Similarity With Sentence-Transformers in Python
James Briggs
33 Sentence Similarity With Transformers and PyTorch (Python)
Sentence Similarity With Transformers and PyTorch (Python)
James Briggs
34 NER With Transformers and spaCy (Python)
NER With Transformers and spaCy (Python)
James Briggs
35 Training BERT #1 - Masked-Language Modeling (MLM)
Training BERT #1 - Masked-Language Modeling (MLM)
James Briggs
36 Training BERT #2 - Train With Masked-Language Modeling (MLM)
Training BERT #2 - Train With Masked-Language Modeling (MLM)
James Briggs
37 Training BERT #3 - Next Sentence Prediction (NSP)
Training BERT #3 - Next Sentence Prediction (NSP)
James Briggs
38 Training BERT #4 - Train With Next Sentence Prediction (NSP)
Training BERT #4 - Train With Next Sentence Prediction (NSP)
James Briggs
39 FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
James Briggs
40 New Features in Python 3.10
New Features in Python 3.10
James Briggs
41 Training BERT #5 - Training With BertForPretraining
Training BERT #5 - Training With BertForPretraining
James Briggs
42 How-to Use HuggingFace's Datasets - Transformers From Scratch #1
How-to Use HuggingFace's Datasets - Transformers From Scratch #1
James Briggs
43 Build a Custom Transformer Tokenizer - Transformers From Scratch #2
Build a Custom Transformer Tokenizer - Transformers From Scratch #2
James Briggs
44 3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
James Briggs
45 3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
James Briggs
46 Building MLM Training Input Pipeline - Transformers From Scratch #3
Building MLM Training Input Pipeline - Transformers From Scratch #3
James Briggs
47 Training and Testing an Italian BERT - Transformers From Scratch #4
Training and Testing an Italian BERT - Transformers From Scratch #4
James Briggs
48 Faiss - Introduction to Similarity Search
Faiss - Introduction to Similarity Search
James Briggs
49 Angular App Setup With Material - Stoic Q&A #5
Angular App Setup With Material - Stoic Q&A #5
James Briggs
50 Why are there so many Tokenization methods in HF Transformers?
Why are there so many Tokenization methods in HF Transformers?
James Briggs
51 Choosing Indexes for Similarity Search (Faiss in Python)
Choosing Indexes for Similarity Search (Faiss in Python)
James Briggs
52 Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
James Briggs
53 How LSH Random Projection works in search (+Python)
How LSH Random Projection works in search (+Python)
James Briggs
54 IndexLSH for Fast Similarity Search in Faiss
IndexLSH for Fast Similarity Search in Faiss
James Briggs
55 Faiss - Vector Compression with PQ and IVFPQ (in Python)
Faiss - Vector Compression with PQ and IVFPQ (in Python)
James Briggs
56 Product Quantization for Vector Similarity Search (+ Python)
Product Quantization for Vector Similarity Search (+ Python)
James Briggs
57 How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
James Briggs
58 Metadata Filtering for Vector Search + Latest Filter Tech
Metadata Filtering for Vector Search + Latest Filter Tech
James Briggs
59 Build NLP Pipelines with HuggingFace Datasets
Build NLP Pipelines with HuggingFace Datasets
James Briggs
60 Composite Indexes and the Faiss Index Factory
Composite Indexes and the Faiss Index Factory
James Briggs

This video teaches how to use custom React components in Streamlit, including setup, installation, and configuration, as well as leveraging Streamlit's caching mechanism to improve performance. It also showcases how to embed React components in Streamlit for interactive visualizations and create a custom dashboard for machine learning models. By following this video, viewers can learn how to create custom and interactive dashboards for their machine learning models.

Key Takeaways
  1. Install Node.js and npm on Mac using Homebrew
  2. Install Streamlit using pip
  3. Clone the Streamlit component template repository from GitHub
  4. Navigate to the template directory and open it in a code editor
  5. Use the template to create a custom component for Streamlit
  6. Rename default file names and directory names
  7. Install node packages for Streamlit component
  8. Install node packages required for card component
  9. Modify init.py to reference Streamlit card component
  10. Use typescript in Streamlit component
💡 By using custom React components in Streamlit, developers can create custom and interactive dashboards for their machine learning models, improving the overall user experience and performance.

Related Reads

Chapters (6)

Intro
2:19 Environment Setup
3:42 Starting with a Template
7:41 Naming for Card Component
11:31 Installing Node Packages
15:12 Running the Component
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →