0% found this document useful (0 votes)
42 views22 pages

Stream Lit

Uploaded by

Its Me SR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views22 pages

Stream Lit

Uploaded by

Its Me SR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

STREAMLIT

Streamlit is a free and open-source framework to rapidly build and share beautiful machine
learning and data science web apps. It is a Python-based library specifically designed for
machine learning engineers. Data scientists or machine learning engineers are not web
developers and they're not interested in spending weeks learning to use these frameworks
to build web apps. Instead, they want a tool that is easier to learn and to use, as long as it
can display data and collect needed parameters for modeling. Streamlit allows you to
create a stunning-looking application with only a few lines of code.

Run Using Streamlit: -


We can run any program using streamlit in windows with the following syntax –
python -m streamlit run ‘file path’
After running streamlit file, we get something like this –

The local URL can be used from the current machine only
The network URL can be used from any machine to see the output file and the changes will
be reflected by refreshing the page.

Import Streamlit: -
We can import streamlit by the import command.
import streamlit as st
Title: -
We can create a title using the title tag.
st.title(‘Enter your text here’)

Header: -
We can create a header using the header tag.
st.header(‘Enter your text here’)

Subheader: -
We can create a subheader using the subheader tag.
st.subheader(‘Enter your text here’)

Text or paragraph: -
We can create a text using the text tag.
st.text(‘Enter your text here’)
Markdown: -
We can write inside markdown function in the same way we written in jupyter notebook.
Here the number of hashes defines the size of the text. More the number of hashes lesser
the size.

st.markdown(‘# Enter your text here’) #big


st.markdown(‘## Enter your text here’) #smaller
st.markdown(‘### Enter your text here’) #smaller
st.markdown(‘#### Enter your text here’) #smallest…

Success: -
We can write any success statement using the success function.
st.success(‘Enter your text here’)

Information: -
We can write any information statement using the info function.
st.info(‘Enter your text here’)
Warning: -
st.warning(‘Enter your text here’)

Error: -
st.error(‘Enter your text here’)

Exception: -
st.exception(‘exception’)
Example –
exp = ZeroDivisionError("Can't divide by 0")
st.exception(exp)

Help: -
The help function is used to see documentation for anything.
st.help(‘write here’)
Write: -
The write function is used to get better formatting for code segment or arithmetic equation
etc.
st.write(‘write here’)

Code: -
The code function is used to write code segments in the webpage.
st.code(‘codeline1\n’
‘codeline2\n’
‘codeline3…’)

Checkbox: -
st.checkbox(‘value1’)
st.checkbox(‘value2’)
st.checkbox(‘value3’)…

We can check if the checkbox is clicked and do some action –


if(st.checkbox(‘value’)):
---statement
Radiobutton: -
st.radio(‘heading: ’,(‘value1’,‘value2’,‘value3’,…))
In this way we can access the radiobutton values –
var = st.radio(‘heading: ’,(‘value1’,‘value2’,‘value3’,…))
if(var == ‘value’):
---statement

Select Box or Dropdown: -


st.selectbox(‘heading:’,[‘value1’,‘value2’,‘value3’,…])
We can access selectbox value using this –
var = st.selectbox(‘heading:’,[‘value1’,‘value2’,‘value3’,…])
st.write(var)
Multi Select Box: -
st.multiselect(‘heading:’,[‘value1’,‘value2’,‘value3’,…])
We can access selectbox value using this –
var = st.multiselect(‘heading:’,[‘value1’,‘value2’,‘value3’,…])
st.write(var)
It will return a dictionary of selected value where the key is the index starting from 0.

Button: -
st.button(‘buttonname’)
Access button click
var = st.button(‘buttonname’)
if(var):
--statement
Slider: -
st.slider(‘heading: ’,start, end, step = no of steps)
Access the slider value by using –
var = st.slider(‘heading: ’,start, end, step = no of steps)
if(var):
---statement

Text Input: -
st.text_input(‘Enter your message’)
The user input value can be accessed by using a variable
var = st.text_input(‘Enter your message’)
if(var):
--statement
Password Input: -
st.text_input(‘Enter your message’, type=‘password’)

Textarea: -
st.text_area(‘Enter your message’)

Input Number: -
st.number_input(‘Enter your message’,start,end)

Input Date: -
st.date_input(‘Enter your message’)
Input Time: -
st.time_input(‘Enter your message’)

Upload .csv file: -


df = st.file_uploader(‘Enter your message’,type=[‘csv’,‘xlsx’])
We can display the uploaded file using the syntax –
if df is not None:
st.dataframe(df)
Upload image: -
img = st.file_uploader(‘Enter your message’,type=[‘png’,‘jpeg’,‘jpg’])
We can display the uploaded file using the syntax –
if img is not None:
st.image(img)

Upload video: -
vid = st.file_uploader(‘Enter your message’,type=[‘mkv,‘mp4’,‘mov’])
We can display the uploaded file using the syntax –
if vid is not None:
st.video(vid)

Here we also have some optional parameters like –

• start_time = secondsvalue → Starts a video from a particular seconds.

Upload Audio: -
aud = st.file_uploader(‘Enter your message’,type=[‘mp3’])
We can display the uploaded file using the syntax –
if aud is not None:
st.audio(aud)
Line Chart: -
For making line chart first we need to make the dataframe. Let’s make the dataframe using
random values using the numpy random function and pandas.

Now we can make the line chart using the function ‘line_chart’
st.line_chart(datafram ename)

Area Chart: -
Using the same data, we can make it’s area chart using the ‘area_chart’ function.
st.area_chart(dataframename)
Bar Chart: -
Using the same data, we can make it’s bar chart using the ‘bar_chart’ function.
st.bar_chart(dataframename)

Bar Graph from csv file data using Matplotlib: -


We can use –
• The ‘figure( )’ function to make a figure.
• ‘value_count( )’ function to count the number of occurrences.
• plot( ) function to plot any type of graphs like bar, histogram, pie chart etc.
• pyplot( ) function to show a matplotlib pyplot figure.

The three species occurs same


number of times so the bar plot
for three of them are exactly
same.
Distribution Plot from csv file data using Seaborn: -
We can use –
• ‘figure( )’ function to make a figure.
• ‘distplot( )’ function to take a specific column from the csv file.
• pyplot( ) function to show a matplotlib pyplot figure.

We are taking the values from


the sepal length field and
making the distribution plot.

Multiple Graphs in one row: -


• The K.D.E means the line in the distribution plot.
• The Hist means the histogram in the distribution plot.
• We can make columns in Streamlit using the function ‘columns( )’
Styling the graphs: -
We can style the graphs using seaborn.
• We can set the style and context using seaborn separately.
sns.set_style(‘stylename’)
sns.set_context(‘contextname’)

• We can set the style and context inside the theme function of seaborn.

sns.set_theme(context=‘contextname’, style=‘stylename’)
Scatter Plot: -
We can make scatter plot by using the scatter function.

Count Plot: -
We can make count plot by using the countplot function under seaborn.

“ df ” is the dataframe name and the ‘species’


is a column under the dataframe.
The count plot needs only one data item.

Box Plot: -
We can make box plot by using the boxplot function under seaborn.

“ df ” is the dataframe name and ‘species’ ,


‘sepal_length’ are the columns under the
dataframe.
The box plot needs two data items.
Violin Plot: -
We can make box plot by using the boxplot function under seaborn.

“ df ” is the dataframe name and ‘species’ ,


‘sepal_length’ are the columns under the
dataframe.
The violin plot needs two data items.

Altair Scatter Plot: -


We can make altair scatter plot by using the Chart function under altair.

We have taken random data of 500 rows and


5 columns then converted it into

For this, we have to import altair as alt.


Interactive Line Chart: -
To make an interactive line Chart first we need to read the columns of the csv file and
convert it into a list. Now from the list we will choose the values using a multiselect
dropdown. Now the selected options are again stored in a variable. Finally we will make a
dataframe from the selected choices and will make the line Chart of that dataframe using
the function ‘line_chart( )’.

from the lang_data.csv we are reading the columns. Then we are selecting the language
from a multiselect dropdown and at the end we are making dataframe from the
selected values.
Interactive Area Chart: -
For the interactive area chart everything will be same as interactive line chart but only the
‘line_chart( )’ function will be replaced as ‘area_chart( )’.

Pie chart using Plotly: -


To use plotly, we have to first install the plotly. Then import something –
import plotly.express as px
import plotly.figure_factory as ff
Now we will use the pie function under plotly express and define which values are used for
making the chart and which are the headers.
Pie chart with multiple parameters using Plotly: -
We can add multiple parameters like opacity, colour in the pie chart to change it’s default
look.

Histogram using Plotly: -


First we will take 3 data elements for random numbers then we will make the list of group
labels. Then we will use the ‘create_distplot( )’ function under plotly.figure_factory which
will take the tuple of 3 data elements, group labels and the size of the bins.
Deploy Using Streamlit
• First we need to go to https://share.streamlit.io/ and sign in using Github.
• In github upload the program files, any other required files and the requirement file.

Under “requirements.txt” we have to write the module names with their versions.
Here we’ve used only Streamlit so we will write only the version of streamlit.

• Now in https://share.streamlit.io/ Press Create App


• Now Select I have an app.

• Now fill all these details and click deploy under the fields.

• Now the file is deployed, and we can find the link in the url bar.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy