Streamlit – Cannot repeat emoji string using st.write()

Question:

Summary:

Normally to repeat a string in python, you’d use (string * n) where n = number of times to repeat.

In Streamlit you have the ability to print emoji using corresponding shortcodes in st.write().

I’ve been able to use st.write(emoji1, emoji2, emoji3) to print emoji successfully, but when I use (string *n) to repeat the emoji string, st.write prints the shortcodes as strings vs printing the actual emoji.

Is this a shortcoming on Streamlit’s code, or am I missing something?

Here’s the link to the app so you can see:

https://halfgingerbeard-30daysofstreamlit-emoji-frame-creator-bsodkg.streamlit.app/

Tried/Expected:

Using a dataframe of Streamlit-accepted emoji shortcodes as df_emoji:

emoji1 = st.select_slider('Select emoji 1:', options = df_emoji['shortcodes'], value=':sunglasses:')
emoji2 = st.select_slider('Select emoji 2:', options = df_emoji['shortcodes'], value=':white_check_mark:')
emoji3 = st.select_slider('Select emoji 3:', options = df_emoji['shortcodes'], value=':coffee:')

e_columns = st.number_input('Select Number of Columns:',1,10,3)

st.write(emoji1, emoji2, emoji3) # this works successfully

st.write((emoji1, emoji2, emoji3) * e_columns)[:e_columns]) # this prints out the string values of the shortcodes instead of the actual emoji

I’m expecting a repeating concatenated string of emoji the length of e_columns, not each shortcode in string format

e.g. ( vs ‘:sunglasses:’)

Asked By: Josh

||

Answers:

First off, you have a syntax error on your last line:

st.write((emoji1, emoji2, emoji3) * e_columns)[:e_columns])
#                                                         ^ unmatched

Add an extra open paren ( just after st.write:

st.write(((emoji1, emoji2, emoji3) * e_columns)[:e_columns])
#       ^ here

Now, in your first call to st.write(), you are passing each individual shortcode as a separate parameter:

st.write(emoji1, emoji2, emoji3) # 3 parameters

However, in the second call just below it, you are passing it a single parameter – a tuple:

st.write(((emoji1, emoji2, emoji3) * e_columns)[:e_columns]) # just one parameter

Try using the unpacking operator * to separate out each emoji into an individual parameter:

st.write(*((emoji1, emoji2, emoji3) * e_columns)[:e_columns])
#        ^

So, if e_columns is 5, for example, from Python’s perspective the actual function call will be

st.write(emoji1, emoji2, emoji3, emoji1, emoji2)
Answered By: MattDMo
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.