Skip to content

Commit b27a7bd

Browse files
author
dbuchaca
committed
feat: streamlist basics
1 parent ab6b1fc commit b27a7bd

5 files changed

Lines changed: 189 additions & 0 deletions

File tree

python_libraries/streamlit/03_widgets.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
"""
9797
st.code(code_ex_4)
9898

99+
### Example 4 BEGIN ###
99100
uploaded_file = None
100101
uploaded_file = st.file_uploader("Upload Image (.png or .jpg only)", type=['png','jpg'], key='uploader_flag')
101102

@@ -104,8 +105,26 @@
104105
image = Image.open(io.BytesIO(bytes_uploaded_file))
105106
image = image.resize((256,256))
106107
st.image(image)
108+
### Example 4 END ###
107109

108110

109111

112+
st.subheader("Text uploader: `st.text_input`")
113+
114+
'The following code will allow you to create a box for writting text'
115+
116+
code_ex_5 = """
117+
uploaded_urls = st.text_input("Paste here urls")
118+
"""
119+
st.code(code_ex_5)
120+
121+
### Example 5 BEGIN ###
122+
uploaded_urls = st.text_input("Paste here urls")
123+
### Example 5 END ###
124+
parsed_urls = uploaded_urls.split('\n')
125+
st.write('Number of uploaded urls are:', len(parsed_urls))
126+
for url in parsed_urls:
127+
st.write('uploaded_urls are:', url)
128+
110129

111130

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# This exapmles shows how to increment a dataframe every time a button is pressed
2+
3+
import streamlit as st
4+
import io
5+
import pandas as pd
6+
7+
df = pd.DataFrame()
8+
st.title("Add Movies and related words to a table")
9+
10+
if 'df' not in st.session_state:
11+
st.session_state.df = pd.DataFrame()
12+
st.session_state.df_csv = None
13+
14+
related_words = None
15+
movie = None
16+
17+
def update_df(st, movie, related_words):
18+
df_current = pd.DataFrame({'movie': movie, 'asin_examples': [related_words] })
19+
st.session_state.df = pd.concat((st.session_state.df, df_current))
20+
st.write(f'Added {len(related_words)} related terms')
21+
st.session_state.df_csv = st.session_state.df.to_csv(index=True).encode("utf-8")
22+
23+
def clear_df():
24+
st.session_state.df = pd.DataFrame()
25+
st.session_state.df_csv = None
26+
27+
new_movie_container = st.container()
28+
new_movie = new_movie_container.text_input('Write here Movie to add')
29+
30+
related_words_container = st.container()
31+
related_words = related_words_container.text_input('related words')
32+
33+
if related_words:
34+
related_words = related_words.split(',')
35+
36+
st.write('Number of movies added so far:', len(st.session_state.df))
37+
added_data = st.button('Add related words',
38+
on_click=update_df,
39+
kwargs=dict(st=st, movie=new_movie, related_words=related_words) )
40+
st.button('Clear data', on_click=clear_df())
41+
42+
if added_data:
43+
download_button = st.download_button("Download button",
44+
data=st.session_state.df.to_csv(index=True).encode("utf-8"),
45+
key='download_button_flag',
46+
file_name='df.csv')
47+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
# This exapmles shows how to increment a dataframe every time a button is pressed
3+
import streamlit as st
4+
import SessionState
5+
import pandas as pd
6+
7+
st.header("Grow table of movies and related words clicking a button")
8+
9+
movies = []
10+
words = []
11+
12+
ss = SessionState.get(movies=movies, words=words)
13+
14+
container_movies = st.container()
15+
container_words = st.container()
16+
17+
new_movie = container_movies.text_input('Write here movie to add')
18+
new_words = container_movies.text_input('Write here related words to movie (separated by a comma)')
19+
add_button = container_words.button('add')
20+
21+
if add_button:
22+
if new_movie in ss.movies:
23+
st.write(f'{new_movie} already in the table')
24+
else:
25+
ss.movies.append(new_movie)
26+
ss.words.append(new_words.split(','))
27+
28+
#st.write('movies:')
29+
#st.write(ss.movies)
30+
#st.write('words:')
31+
#st.write(ss.words)
32+
33+
st.header("Table")
34+
35+
movies = ss.movies
36+
words = ss.words
37+
df = pd.DataFrame({'movie': movies, 'words': words})
38+
st.dataframe(df)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This exapmles shows how to increment a dataframe every time a button is pressed
2+
3+
import streamlit as st
4+
import SessionState
5+
6+
st.title("Add words to a list when clicking a button")
7+
8+
words = []
9+
10+
ss = SessionState.get(words=words)
11+
12+
container1 = st.container()
13+
new_word = container1.text_input('Write here word to add')
14+
container2 = st.container()
15+
add_button = container2.button('add')
16+
17+
if add_button:
18+
ss.words.append(new_word)
19+
20+
st.write(ss.words)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from streamlit.report_thread import get_report_ctx
2+
import streamlit as st
3+
4+
5+
class SessionState(object):
6+
def __init__(self, **kwargs):
7+
"""A new SessionState object.
8+
9+
Parameters
10+
----------
11+
**kwargs : any
12+
Default values for the session state.
13+
14+
Example
15+
-------
16+
>>> session_state = SessionState(user_name='',
17+
favorite_color='black')
18+
>>> session_state.user_name = 'Mary'
19+
''
20+
>>> session_state.favorite_color
21+
'black'
22+
23+
"""
24+
for key, val in kwargs.items():
25+
setattr(self, key, val)
26+
27+
28+
@st.cache(allow_output_mutation=True)
29+
def get_session(id, **kwargs):
30+
return SessionState(**kwargs)
31+
32+
33+
def get(**kwargs):
34+
"""Gets a SessionState object for the current session.
35+
36+
Creates a new object if necessary.
37+
38+
Parameters
39+
----------
40+
**kwargs : any
41+
Default values you want to add to the session state, if we're
42+
creating a
43+
new one.
44+
45+
Example
46+
-------
47+
>>> session_state = get(user_name='', favorite_color='black')
48+
>>> session_state.user_name
49+
''
50+
>>> session_state.user_name = 'Mary'
51+
>>> session_state.favorite_color
52+
'black'
53+
54+
Since you set user_name above, next time your script runs this will be
55+
the
56+
result:
57+
>>> session_state = get(user_name='', favorite_color='black')
58+
>>> session_state.user_name
59+
'Mary'
60+
61+
"""
62+
ctx = get_report_ctx()
63+
id = ctx.session_id
64+
return get_session(id, **kwargs)
65+

0 commit comments

Comments
 (0)