Streamlit web development concept
💻💻Streamlit documentation💻💻Streamlit is an open-source Python library that allows developers to create interactive, web-based data applications with minimal effort. Below is an overview of Streamlit's documentation, including examples and functions.
Streamlit Overview:-
Streamlit is designed to make it easy to build and share data apps. It integrates seamlessly with Python scripts and provides a simple way to convert data scripts into interactive web applications.
Installation
First, you need to install Streamlit:
shCopy codepip install streamlit
Running a Streamlit App:-
To run a Streamlit app, create a Python script (e.g., app.py) and use the following command:
shopy codestreamlit run app.py
Basic Example:-
Here's a basic example of a Streamlit app:
pythonopy codeimport streamlit as st
import pandas as pd
import numpy as np
# Title
st.title('My First Streamlit App')
# Header
st.header('Introduction to Streamlit')
# Subheader
st.subheader('This is a subheader')
# Text
st.text('This is a simple text.')
# Markdown
st.markdown('**Markdown** text is also supported.')
# Dataframe
df = pd.DataFrame(
np.random.randn(10, 5),
columns=('col %d' % i for i in range(5))
)
st.dataframe(df)
# Chart
st.line_chart(df)
# Slider
x = st.slider('Select a value', 0, 100, 50)
st.write('Selected value:', x)
Key Streamlit Functions
Here are some of the key functions provided by Streamlit, along with their descriptions and examples.
Layout and Containers
st.title(title): Display a title.pythonCopy codest.title('This is a title')st.header(header): Display a header.pythonCopy codest.header('This is a header')st.subheader(subheader): Display a subheader.pythonCopy codest.subheader('This is a subheader')st.text(text): Display text.pythonCopy codest.text('This is some text')st.markdown(body): Display markdown text.pythonCopy codest.markdown('# This is a markdown heading')st.write(*args): Display various objects like text, markdown, dataframes, and more.pythonCopy codest.write('Hello, world!', 1234, {'key': 'value'})
Displaying Data
st.dataframe(data): Display a dataframe as an interactive table.pythonCopy codeimport pandas as pd st.dataframe(pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]}))st.table(data): Display a static table.pythonCopy codest.table(pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]}))st.json(data): Display JSON-encoded data.pythonCopy codest.json({'key': 'value', 'numbers': [1, 2, 3, 4]})
Charts
st.line_chart(data): Display a line chart.pythonCopy codeimport numpy as np st.line_chart(np.random.randn(10, 2))st.area_chart(data): Display an area chart.pythonCopy codest.area_chart(np.random.randn(10, 2))st.bar_chart(data): Display a bar chart.pythonCopy codest.bar_chart(np.random.randn(10, 2))
Interactive Widgets
st.button(label): Display a button.pythonCopy codeif st.button('Click me'): st.write('Button clicked!')st.checkbox(label): Display a checkbox.pythonCopy codeif st.checkbox('Check me'): st.write('Checkbox checked!')st.radio(label, options): Display a set of radio buttons.pythonCopy codechoice = st.radio('Pick one', ['A', 'B', 'C']) st.write('You selected:', choice)st.selectbox(label, options): Display a select box.pythonCopy codeoption = st.selectbox('Pick one', ['A', 'B', 'C']) st.write('You selected:', option)st.multiselect(label, options): Display a multi-select box.pythonpy codeoptions = st.multiselect('Select multiple', ['A', 'B', 'C']) st.write('You selected:', options)st.slider(label, min_value, max_value, value): Display a slider.pythonpy codevalue = st.slider('Pick a number', 0, 100, 50) st.write('Slider value:', value)st.text_input(label): Display a text input box.python codetext = st.text_input('Enter some text') st.write('You entered:', text)st.text_area(label): Display a text area.pythoy codetext = st.text_area('Enter longer text') st.write('You entered:', text)st.date_input(label): Display a date input widget.pythonopy codedate = st.date_input('Pick a date') st.write('Selected date:', date)st.time_input(label): Display a time input widget.pythonopy codetime = st.time_input('Pick a time') st.write('Selected time:', time)st.file_uploader(label): Display a file uploader.pythonopy codefile = st.file_uploader('Upload a file') if file: st.write('File uploaded:', file.name)st.color_picker(label): Display a color picker.pythonopy codecolor = st.color_picker('Pick a color') st.write('Selected color:', color)
Advanced Usage
Customizing Layout
st.sidebar: Place widgets and text in a sidebar.pythonpy codest.sidebar.title('Sidebar Title') st.sidebar.button('Sidebar Button')st.beta_expander(label): Create an expandable/collapsible section.pythonopy codewith st.beta_expander('See explanation'): st.write('Here is the explanation...')st.beta_columns(spec): Create columns to organize layout.pythonodecol1, col2, col3 = st.beta_columns(3) col1.button('Button 1') col2.button('Button 2') col3.button('Button 3')
State Management
Streamlit allows you to manage state using session state, which can be useful for maintaining values across reruns.
pythoncodeif 'key' not in st.session_state:
st.session_state['key'] = 'Initial value'
st.write(st.session_state['key'])
st.session_state['key'] = st.text_input('Update value', st.session_state['key'])
Further Resources
For comprehensive details and the latest updates, refer to the official Streamlit documentation:
These resources will provide you with the most up-to-date and detailed information on using Streamlit effectively.
import streamlit as st
import pandas as pd
st.title('Streamlit Dashboard project')
st.header('We are just like a friend')
st.subheader('Ka ho raja')
st.write('This is a Normal text')
# use this for write the formula in standard format.
st.latex('x^2 + b^2 + 2ab')
st.markdown('''
### My Favourite movie
-Race3
-Humsufar
-Houseful
''')
# For write the code same as
st.code('''
def square(input):
return square**2
x=square(2)
''')
df = pd.DataFrame({
'Name': ['Ravi', 'Mukesh', 'Kishan', 'Ankit'],
'Marks': [90, 80, 70, 60],
'Packages': [30, 20, 15, 7]
})
st.dataframe(df)
# revenue
st.metric('Revenue', 'Rs 3L', '-3%')
# For json
st.json({
'Name': ['Ravi', 'Mukesh', 'Kishan', 'Ankit'],
'Marks': [90, 80, 70, 60],
'Packages': [30, 20, 15, 7]
})
# Display media
# image
st.image('ravi.jpg')
st.video('WhatsApp Video 2024-05-01 at 12.02.53 PM.mp4')
# sidebar
st.sidebar.title('Ye hi hai sise bar dekho')
st.sidebar.video('WhatsApp Video 2024-05-01 at 12.02.53 PM.mp4')
st.sidebar.image('ravi.jpg')
#if i want to make a collumns side by side then.
col1, col2 = st.columns(2)
with col1:
st.image('ravi.jpg')
with col2:
st.video('WhatsApp Video 2024-05-01 at 12.02.53 PM.mp4')
☝☝ Form of login password with create button ☝☝
import streamlit as st
import pandas as pd
email=st.text_input('Enter email')
password = st.text_input('Enter the password')
btn=st.button('Beta login kara ho')
if btn:
if email == 'rsinghlax@gmail.com' and password == '12345':
st.success('Login successful')
else:
st.error('Login failed')


Comments