[英]Streamlit - Cannot repeat emoji string using st.write()
Summary:概括:
Normally to repeat a string in python, you'd use (string * n) where n = number of times to repeat.通常要在 Python 中重复一个字符串,您会使用 (string * n),其中 n = 重复次数。
In Streamlit you have the ability to print emoji using corresponding shortcodes in st.write()
.在 Streamlit 中,您可以使用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.我已经能够使用st.write(emoji1, emoji2, emoji3)
成功打印表情符号,但是当我使用 (string *n) 重复表情符号字符串时,st.write 将简码打印为字符串而不是打印实际的表情符号.
Is this a shortcoming on Streamlit's code, or am I missing something?这是 Streamlit 代码的缺点,还是我遗漏了什么?
Here's the link to the app so you can see:这是该应用程序的链接,因此您可以看到:
https://halfgingerbeard-30daysofstreamlit-emoji-frame-creator-bsodkg.streamlit.app/ https://halfgingerbeard-30daysofstreamlit-emoji-frame-creator-bsodkg.streamlit.app/
Tried/Expected:尝试/预期:
Using a dataframe of Streamlit-accepted emoji shortcodes as df_emoji:使用 Streamlit 接受的表情符号短代码的数据框作为 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_columns 的长度重复连接的表情符号字符串,而不是字符串格式的每个简码
eg ( vs ':sunglasses:')例如(与':太阳镜:')
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
之后:
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()
时,您将每个单独的短代码作为单独的参数传递:
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因此,如果e_columns
是 5,例如,从 Python 的角度来看,实际的函数调用将是
st.write(emoji1, emoji2, emoji3, emoji1, emoji2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.