當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Streamlit st.echo用法及代碼示例

在`with` 塊中使用以在應用程序上繪製一些代碼,然後執行它。

函數簽名

st.echo(code_location="above")
參數說明

code_location ("above" or "below")

是否在執行代碼塊的結果之前或之後顯示回顯代碼。

示例

with st.echo():
    st.write('This code will be printed')

顯示代碼

有時您希望您的 Streamlit 應用程序包含 both 您常用的 Streamlit 圖形元素 and 生成這些元素的代碼。這就是st.echo() 的用武之地。

好的,假設您有以下文件,並且您希望通過在 Streamlit 應用程序中顯示中間部分來使其應用程序更加不言自明:

import streamlit as st

def get_user_name():
    return 'John'

# ------------------------------------------------
# Want people to see this part of the code...

def get_punctuation():
    return '!!!'

greeting = "Hi there, "
user_name = get_user_name()
punctuation = get_punctuation()

st.write(greeting, user_name, punctuation)

# ...up to here
# ------------------------------------------------

foo = 'bar'
st.write('Done!')

上麵的文件創建了一個 Streamlit 應用程序,其中包含“Hi there, John”,然後是 "Done!"。

現在讓我們使用st.echo() 使代碼的中間部分在應用程序中可見:

import streamlit as st

def get_user_name():
    return 'John'

with st.echo():
    # Everything inside this block will be both printed to the screen
    # and executed.

    def get_punctuation():
        return '!!!'

    greeting = "Hi there, "
    value = get_user_name()
    punctuation = get_punctuation()

    st.write(greeting, value, punctuation)

# And now we're back to _not_ printing to the screen
foo = 'bar'
st.write('Done!')

that 很簡單!

push_pin

注意

您可以在同一個文件中有多個 st.echo() 塊。隨心所欲地使用它!

相關用法


注:本文由純淨天空篩選整理自streamlit.io大神的英文原創作品 st.echo。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。