Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何根据用户答案在Streamlight中显示代码?_Python_Inspect_Streamlit - Fatal编程技术网

Python 如何根据用户答案在Streamlight中显示代码?

Python 如何根据用户答案在Streamlight中显示代码?,python,inspect,streamlit,Python,Inspect,Streamlit,我正在尝试为具有Streamlight的库创建教程。我的总体想法是浏览不同的函数和类,并用基于用户的输入一起解释它们,这样对初学者来说一切都变得更容易理解。 但是,我之前为经验丰富的用户编写了5个教程,我希望通过在我的应用程序中调用这些代码来重用其中的一些代码,并且只维护一次 此外,我还浏览了很多函数和类,例如配置文件示例,并从dict调用它们 由于Streamlit为st.echo提供了一个运行代码然后显示代码的选项,我已经尝试过了。我还尝试将python inspect元素与st.write

我正在尝试为具有Streamlight的库创建教程。我的总体想法是浏览不同的函数和类,并用基于用户的输入一起解释它们,这样对初学者来说一切都变得更容易理解。 但是,我之前为经验丰富的用户编写了5个教程,我希望通过在我的应用程序中调用这些代码来重用其中的一些代码,并且只维护一次

此外,我还浏览了很多函数和类,例如配置文件示例,并从dict调用它们

由于Streamlit为st.echo提供了一个运行代码然后显示代码的选项,我已经尝试过了。我还尝试将python inspect元素与st.write一起使用。但是,st.echo只显示函数名,而st.write和inspect只显示字符串


display_code = st.radio("Would you like to display the code?", ("Yes", "No"))

    if display_code == "Yes":
        with st.echo():
            example_function_1()



    else:
        example_function_1()
基本上,我正在寻找一个选项来传递函数,并根据用户输入简单地运行它,或者运行它并显示代码和注释

因此,如果用户选择“是”,则输出为,同时返回x,y

def example_function_1():
     """ 
     This is and example functions that is now displayed. 
     """ 
     Some Magic
     return x, y 


如果用户选择否,则只返回x,y

您可以使用会话状态将用户输入传递到屏幕上的操作。可以找到单选按钮的清晰示例。一般来说,您需要使用st.write()来完成此任务。带有滑块的简化示例:

import streamlit as st

x = st.slider('Select a value')
st.write(x, 'squared is', x * x)
您所寻找的并不完全可能,因为您必须在with st.echo()块中指定函数。你可以在这里看到:

import inspect
import streamlit as st

radio = st.radio(label="", options=["Yes", "No"])

if radio == "Yes":
    with st.echo():
        def another_function():
            pass
        # Print and execute function
        another_function()
elif radio == "No":
    # another_function is out of scope here..
    another_function()

下面是Streamlit的
st.echo()
的修改版本,其中包含一个复选框:

import contextlib
import textwrap
import traceback

import streamlit as st
from streamlit import source_util


@contextlib.contextmanager
def maybe_echo():
    if not st.checkbox("Show Code"):
        yield
        return

    code = st.empty()
    try:
        frame = traceback.extract_stack()[-3]
        filename, start_line = frame.filename, frame.lineno

        yield

        frame = traceback.extract_stack()[-3]
        end_line = frame.lineno
        lines_to_display = []
        with source_util.open_python_file(filename) as source_file:
            source_lines = source_file.readlines()
            lines_to_display.extend(source_lines[start_line:end_line])
            initial_spaces = st._SPACES_RE.match(lines_to_display[0]).end()
            for line in source_lines[end_line:]:
                indentation = st._SPACES_RE.match(line).end()
                # The != 1 is because we want to allow '\n' between sections.
                if indentation != 1 and indentation < initial_spaces:
                    break
                lines_to_display.append(line)
        lines_to_display = textwrap.dedent("".join(lines_to_display))

        code.code(lines_to_display, "python")

    except FileNotFoundError as err:
        code.warning("Unable to display code. %s" % err)

谢谢,但我知道如何使用用户输入。我的问题是如何在仍然运行代码的情况下,在基于它的函数中显示代码。你的意思是想“打印/回显”函数的代码吗?确切地说,我想从另一个模块调用函数,同时运行和“打印”它。我使用了复选框而不是无线电组,因为只有两个选项,但您可以将其修改为使用
st.radio
with maybe_echo():
    some_computation = "Hello, world!"
    st.write(some_computation)