【问题标题】:Streamlit throwing error list index out of rangeStreamlit 抛出错误列表索引超出范围
【发布时间】:2021-08-31 09:55:48
【问题描述】:

我正在使用 streamlit 创建应用程序。每当我从选择菜单中选择一个选项时,它都会引发错误:

IndexError:列表索引超出范围

追溯:

python\python39\lib\site-packages\streamlit\script_runner.py", line 338, in _run_script
exec(code, module.__dict__)

in <module> get_option(option)

in get_option result = summarise_video(article_url)

in summarise_video video_id = video_url.split("=")[1]

但是当我提供数据时它工作正常。

这里是代码

def get_option(option):
    if option == "Text Summarisation":
        sentence = st.text_area('Enter your text here...')
        st.button('Summarise')
        if st.button:
            st.write("Your Summary")
            result = summarise_text(sentence)
            st.write(result)
    elif option == "Video Summarisation":
        video_url = st.text_input('Enter youtube url here...')
        st.button('Summarise')
        if st.button:
            result = summarise_video(video_url)
            st.write(result)
    else:
        article_url = st.text_input('Enter article url here...')
        st.button('Summarise')
        if st.button:
            result = summarise_video(article_url)
            st.write(result)


option = st.sidebar.selectbox(
    "Select option",  ("Text Summarisation", "Web Scrapping", "Video Summarisation"))

get_option(option)```


Any help would be appreciated. Thanks in advance.

【问题讨论】:

  • video_url.split("=")[1] video_url 为空或不包含=。这不是流光相关的错误,这是函数 summarise_video 中的逻辑错误。
  • 我试过调试它。视频网址包含值以及“=”
  • 代码也正常运行并给出了预期的结果。只是在输入 url 之前,它显示了这个错误。
  • 堆栈跟踪告诉您summarise_video 在尝试拆分字符串时会引发错误。你能发布你的“总结视频”方法吗?还。输入视频 URL 后立即将其打印出来,并告诉我您为该视频获得的值youtube.com/watch?v=dQw4w9WgXcQ
  • 哦。没关系。只需阅读“代码也正常运行并给出了预期的结果。只是在输入 url 之前,它显示了这个错误。”在这种情况下...代码按预期执行。

标签: python streamlit


【解决方案1】:

我认为这是一个逻辑错误,而不是流线型错误。您没有检查您的 url 链接是否有效。我创建了一个小功能来检查它是否是 youtube 视频。 (你可能想最终创造一个更好的)

import requests
def is_youtube_url(url:str) -> bool:
    try:    
        #Checks if it's a youtube url
        if "youtube.com/watch" not in url: 
            return False
        #Checks if the URL is a valid video
        r = requests.get("https://youtube.com/watch?v=dQw4w9WgXcQ") # random video id
        is_available = "Video unavailable" in r.text
        return is_available
    except:
        return False

另外。您不是在调用st.button,而是在 if 语句中引用该函数。例如,您应该编写is_button_clicked = st.button("Run"),然后检查变量is_button_clicked 的真值。相反,您正在检查 st.button 的真值,它是一个函数,因此它将始终返回 true。

像这样改变你的陈述。

    else:
        article_url = st.text_input('Enter article url here...')
        is_summarized_button_clicked = st.button('Summarise')
        if is_summarized_button_clicked:
            if is_youtube_url(article_url):
                result = summarise_video(article_url)
                st.write(result)  
            else:  
                st.error("The URL you entered is not a valid youtube URL")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-25
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多