
streamlit authenticator 库近期升级后,`authenticator.login()` 方法的参数签名发生变更:原 `form_name` 参数已被弃用,需改用 `fields` 字典传入表单标题等自定义字段,否则将触发 `deprecationerror`。
streamlit Authenticator 是广泛用于 Streamlit 应用中快速实现基础身份验证的第三方库。但自 v0.4.0 起(尤其是 v0.5.0+ 版本),其核心登录接口 authenticator.login() 进行了向后不兼容的重构:form_name 参数已被正式移除,取而代之的是更灵活、可扩展的 fields 参数。这一变更旨在支持多语言、自定义字段标签及未来 ui 扩展能力。
若你仍沿用旧写法(如 authenticator.login(‘Login’, ‘main’)),运行时将抛出明确的 DeprecationError,提示:
Likely deprecation error, the 'form_name' parameter has been replaced with the 'fields' parameter.
✅ 正确用法如下(推荐显式指定所有关键字段):
def Login(): authenticator = Authenticate( config['credentials'], config['cookie']['name'], config['cookie']['key'], config['cookie']['expiry_days'], config['preauthorized'] ) # ✅ 新写法:使用 fields 字典替代 form_name 参数 name, authentication_status, username = authenticator.login( location='main', # 原 'form_name' 的位置参数现为 location(固定值 'main' 或 'sidebar') fields={ 'Form name': 'Login', # 表单顶部标题(必填,键名固定) 'Username': 'Username', # 用户名输入框标签(可本地化) 'Password': 'Password', # 密码输入框标签 'Login': 'Sign in' # 登录按钮文字 } ) if authentication_status: st.title('title of page') AfterLoginConfig() AfterLoginTopMenu() AfterLogin() elif authentication_status is False: st.error('Username/password is incorrect') elif authentication_status is None: st.warning('Please enter your username and password')
? 关键说明:
- location 是第一个位置参数(非关键字),仅接受 ‘main’ 或 ‘sidebar’,用于指定登录表单渲染位置;
- fields 是必需的关键字参数(dict 类型),其中 ‘Form name’ 键为必填项(即使值与旧 form_name 相同),其余键(如 ‘Username’, ‘Password’, ‘Login’)均可按需自定义,便于国际化或品牌化;
- authentication_status 的判断建议使用 is True / is False / is None(而非 == True),避免潜在布尔值比较陷阱;
- 若未传入 fields,库将回退至默认英文标签,但显式声明更安全、可维护。
? 补充建议:
- 升级后务必检查 streamlit-authenticator 版本:运行 pip show streamlit-authenticator,推荐使用 ≥ v0.5.2;
- 可通过 pip install –upgrade streamlit-authenticator 更新至最新稳定版;
- 官方文档与示例已同步更新,详见:https://www.php.cn/link/3e9ac32bbcbe9c84d3c740a92783f692
此次变更虽带来短期适配成本,但显著提升了组件的可定制性与长期可维护性。只需一次代码调整,即可确保登录功能持续稳定运行。