You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用streamlit_login_auth_ui的get_username()函数出现属性错误

解决streamlit_login_auth_ui中get_username()属性不存在的问题

问题背景

使用streamlit_login_auth_ui 0.2.0版本构建登录界面时,尝试调用__login__实例的get_username()方法获取用户名以实现管理员权限控制,触发如下错误:

AttributeError: '__login__' object has no attribute 'get_username'

问题原因

该库在0.2.0版本中已移除get_username()方法,改为通过build_login_ui()方法返回**(登录状态, 用户名)**的元组来传递当前登录用户信息,原代码仍使用已废弃的方法导致报错。

修复后的完整代码

import streamlit as st  
from streamlit_login_auth_ui.widgets import __login__

__login__obj = __login__(auth_token = st.secrets['courier_auth_token'], 
                    company_name = "The Suite",
                    width = 200, height = 250, 
                    logout_button_name = 'Logout',
                    hide_menu_bool = False, 
                    hide_footer_bool = True, 
                    lottie_url = 'https://assets2.lottiefiles.com/packages/lf20_jcikwtux.json'
                    )

# 接收build_login_ui返回的登录状态与用户名
LOGGED_IN, username = __login__obj.build_login_ui()

if LOGGED_IN != True:
    with open('style3.css') as f:
        st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)

if LOGGED_IN == True:
    st.sidebar.success('Select a page above')

    # 直接使用获取到的username进行权限判断
    if username =='Liscivia':
        def show_download_Json():
            with open('_secret_auth_.json') as f:
                st.download_button('Download Json', f)

        show_download_Json()

核心修改说明

  • 调整build_login_ui()的赋值方式,同时接收登录状态和用户名两个返回值
  • 移除错误的username = __login__obj.get_username()调用,直接使用从build_login_ui()获取的username变量

内容的提问来源于stack exchange,提问作者Liscivia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 01:05:20