debugging 通过SystemExit简化错误处理:0

jc3wubiy  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(145)

上下文:

我有以下实现@click.command的代码,我以编程方式调用该命令,并希望使用st.header将输出显示为流式标题

import streamlit as st
import click

st.header("hi")

@click.command()
@click.option("--rating", type=int, default=5, help="The rating of the movie")
@click.argument("like")
def show_rating(rating, like):
    print(f"The movie has a rating of {rating}")
    print(f"The movie is likeable: {like}")

st.header(show_rating(["True", "--rating", "8", ]))

由于某种原因,streamlit错误输出,并显示以下错误:

SystemExit: 0
Traceback:
File "d:\program files\anaconda3\lib\site-packages\streamlit\script_runner.py", line 333, in _run_script
    exec(code, module.__dict__)
File "D:\Users\Jamil\Desktop\Python\Meta\Streamlit\ui.py", line 16, in <module>
    st.header(show_rating(["True", "--rating", "8", ]))
File "d:\program files\anaconda3\lib\site-packages\click\core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
File "d:\program files\anaconda3\lib\site-packages\click\core.py", line 1081, in main
    sys.exit(e.exit_code)

我把错误缩小到这一行

st.header(show_rating(["True", "--rating", "8", ]))

为什么streamlit出现错误,而terminal在打印时显示结果?

终端输出:

blmhpbnm

blmhpbnm1#

因为您向show_rating传递了一个错误的参数,该参数位于列表的最后。
变更:

st.header(show_rating(["True", "--rating", "8", ]))

收件人:

st.header(show_rating(["True", "--rating", "8"]))

相关问题