简体   繁体   English

Streamlit - 无法使用 st.write() 重复表情符号字符串

[英]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.

相关问题 是否可以在 streamlit 中创建像这样的不可编辑的文本框? St.write 只会写,但我需要在文本框中 - Is it possible to create a non-editable text box like this in streamlit ? St.write would only write but i need it in a text box Streamlit - 无法实例化协议 - Streamlit - Protocols cannot be instantiated 我如何将 st.columns 与 st.tabs 一起使用(Streamlit) - How can ı use st.columns with st.tabs (Streamlit) 将使用命令os.lstat(“ some_file。txt”)。st_size阻止文件,以便其他程序无法写入? - will using the command os.lstat(“some_file. txt”).st_size block the file so other programs cannot write? 如何使用 python 将多个数据帧写入 excel 并使用 streamlit 下载结果? - How do I write multiple dataframes to excel using python and download the results using streamlit? Streamlit 无法导入 sklearn 模块 - Streamlit cannot import sklearn module 创建字符串 st =“HELLO”。 使用循环 - create string st ="HELLO". using loop streamlit st.info 信息框中的文本对齐 - Text align in streamlit st.info information box KeyError: 'st.session_state 在 Streamlit 中没有密钥 - KeyError: 'st.session_state has no key in Streamlit 错误:无效的要求:'import streamlit as st' - requirements.txt - ERROR: Invalid requirement: 'import streamlit as st' - requirements.txt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM