【问题标题】:Show some fix point on a slider in streamlit app在 streamlit 应用程序的滑块上显示一些固定点
【发布时间】:2023-02-24 22:04:23
【问题描述】:

有没有办法在 streamlit 应用程序的滑块上显示一些固定点?我一直在寻找它,但我找不到任何文档或示例。

【问题讨论】:

    标签: python streamlit


    【解决方案1】:

    是的,可以使用 Streamlit 应用程序在滑块上显示固定点格式函数的参数滑块功能。

    这是一个演示如何实现此目的的示例:

    import streamlit as st
    
    # Define the range of the slider
    min_val = 0
    max_val = 100
    
    # Define the fixed point you want to display on the slider
    fixed_point = 50
    
    # Define a function to format the slider value
    def format_slider_value(val):
        if val == fixed_point:
            return f"{val} (fixed point)"
        else:
            return str(val)
    
    # Create the slider with the `format_func` parameter
    slider_val = st.slider("Select a value", min_val, max_val, format_func=format_slider_value)
    
    # Display the selected value
    st.write("You selected:", slider_val)
    

    在这个例子中,format_slider_value 函数如果滑块值与定点值匹配,则通过将字符串“(定点)”附加到值来格式化滑块值。这滑块函数然后使用这个函数作为格式函数在滑块上显示格式化值的参数。当用户选择滑块上的固定点时,滑块值将显示为“50(固定点)”。

    我希望这有帮助!

    【讨论】:

    • 谢谢,我运行了你的代码,但错误是 TypeError: slider() got an unexpected keyword argument 'format_func' ,你知道吗?
    • @user14269252 你的 Streamlite 是什么版本?
    • 是最新版本,代码适合你吗?
    • 让我检查一下,我会尽快更新。
    • format_func 对我有用 st.multiselect 但滑块没有。
    猜你喜欢
    • 2021-08-30
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    相关资源
    最近更新 更多