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