将使用file_selector上传的内容使其在main.py中可用的推荐方法是什么?

huangapple go评论68阅读模式
英文:

What is the recommended way to make uploaded content using file_selector available to main.py?

问题

以下是您要的代码部分的中文翻译:

# 我正在尝试使用file_selector来上传一个yaml文件,并希望将上传的内容提供给我的main.py程序,并在我的网页上查看内容。目前,我定义了一个保存读取内容的全局变量,如下所示:

from taipy.gui import Gui
import yaml

parsed_data = dict()  # 存储解析后的数据

path = None  # 文件路径

# 创建file_selector组件,用于上传yaml文件
md = "<|{path}|file_selector|label=上传文件|on_action=load_yaml_file|extensions=.yaml|>"

# 定义文件加载函数
def load_yaml_file(state):
    global parsed_data  # 使用全局变量

    with open(state.path, 'r') as file:
        yaml_data = file.read()

    # 解析YAML数据为字典
    parsed_data = yaml.safe_load(yaml_data)

# 其他操作

Gui(md).run()  # 运行界面

有没有使用全局变量的其他选项?

英文:

I am experimenting with file_selector to upload a yaml file and want to make the uploaded content available to my main.py program and view the content on my webpage. Currently I defined a global variable that holds the read content like below:

from taipy.gui import Gui
import yaml

parsed_data = dict()

path = None

md = "<|{path}|file_selector|label=Upload file|on_action=load_yaml_file|extensions=.yaml|>"

def load_yaml_file(state):
    global parsed_data

    with open(state.path, 'r') as file:
        yaml_data = file.read()

    # Parse the YAML data into a dictionary
    parsed_data = yaml.safe_load(yaml_data)

# other stuff

Gui(md).run()

What are other options instead of using a global variable?

答案1

得分: 1

你应该使用状态来显示你的变量。

查看这个文档部分以了解状态。

状态保存了在一个特定连接中用于用户界面的所有变量的值。

from taipy.gui import Gui
import yaml

parsed_data = dict()
parsed_data_str = ""

path = None

md = """
<|{path}|file_selector|label=Upload file|on_action=load_yaml_file|extensions=.yaml|>

<|{parsed_data_str}|input|active=False|multiline|>
"""

def load_yaml_file(state):
        with open(state.path, 'r') as file:
            yaml_data = file.read()

        # Parse the YAML data into a dictionary
        state.parsed_data = yaml.safe_load(yaml_data)
        state.parsed_data_str = yaml_data # you will see your yaml file as a string

# other stuff

Gui(md).run()

现在,你可以使用你放进去的内容来检索 state.parsed_datastate 总是 Taipy 回调函数的一个参数。

我还尝试在页面上包含答案。

英文:

You should use the state to display your variables.

Check out this section of the documentation to understand the state.

> The state holds the value of all the variables used in the user
> interface for one specific connection.

from taipy.gui import Gui
import yaml

parsed_data = dict()
parsed_data_str = ""

path = None

md = """
<|{path}|file_selector|label=Upload file|on_action=load_yaml_file|extensions=.yaml|>

<|{parsed_data_str}|input|active=False|multiline|>
"""

def load_yaml_file(state):
        with open(state.path, 'r') as file:
            yaml_data = file.read()

        # Parse the YAML data into a dictionary
        state.parsed_data = yaml.safe_load(yaml_data)
        state.parsed_data_str = yaml_data # you will see your yaml file as a string

# other stuff

Gui(md).run()

Now, you can retrieve state.parsed_data with what you have put inside. The state is always a parameter of callbacks in Taipy.

I also tried to include the answer on the page.

huangapple
  • 本文由 发表于 2023年7月27日 20:12:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定