Streamlit 数据框中的超链接
我试图在包含 Streamlit 上过滤结果的数据框中显示可单击的超链接。到目前为止,这是我的代码:
import pandas as pd
import streamlit as st
import openpyxl
import numpy as np
from IPython.core.display import display, HTML
df = pd.read_excel(
io='list.xlsx',
engine= 'openpyxl',
).fillna('')
def make_clickable(link):
# target _blank to open new window
# extract clickable text to display for your link
text = link.split('=')[0]
return f'<a target="_blank" href="{link}">{text}</a>'
# TRAILER is the column with hyperlinks
df['TRAILER'] = df['TRAILER'].apply(make_clickable)
df['TRAILER'] = HTML(display(df.to_html(render_links=True, escape=False), raw=True))
如果我使用:
df['TRAILER'] = df['TRAILER'].apply(make_clickable)
我会
<a target="_blank" href="{link}">{text}</a>
显示为字符串,而不是超链接。
当我添加:时
df['TRAILER'] = HTML(display(df.to_html(render_links=True, escape=False), raw=True))
,我得到:
<IPython.core.display.HTML object>
显示为字符串,而不是超链接。
这些是我正在使用的版本。该网站的其他组件仅适用于较低版本的 Streamlit,这就是我使用此版本的原因。
streamlit == 0.83 numpy == 1.18 pandas == 1.2 openpyxl ipython == 7.22 python == 3.9
我无法使用 st.markdown
或 st.write
直接,因为我使用 st.dataframe
显示结果。
I am attempting to display a clickable hyperlink inside a dataframe containing filtered results on Streamlit. This is my code so far:
import pandas as pd
import streamlit as st
import openpyxl
import numpy as np
from IPython.core.display import display, HTML
df = pd.read_excel(
io='list.xlsx',
engine= 'openpyxl',
).fillna('')
def make_clickable(link):
# target _blank to open new window
# extract clickable text to display for your link
text = link.split('=')[0]
return f'<a target="_blank" href="{link}">{text}</a>'
# TRAILER is the column with hyperlinks
df['TRAILER'] = df['TRAILER'].apply(make_clickable)
df['TRAILER'] = HTML(display(df.to_html(render_links=True, escape=False), raw=True))
If I use:
df['TRAILER'] = df['TRAILER'].apply(make_clickable)
I get
<a target="_blank" href="{link}">{text}</a>
displayed as a string but not a hyperlink.
When I add:
df['TRAILER'] = HTML(display(df.to_html(render_links=True, escape=False), raw=True))
I get:
<IPython.core.display.HTML object>
displayed as a string but not a hyperlink.
These are the versions I am using. Other components of the site work only with a lower version of Streamlit which is why I am using this version.
streamlit == 0.83 numpy == 1.18 pandas == 1.2 openpyxl ipython == 7.22 python == 3.9
I cannot use st.markdown
or st.write
directly as I am using st.dataframe
to display the results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
st.write(df.to_html(escape=False, index=False), unsafe_allow_html=True)
:在一个工作示例中:
它给出:
You can use
st.write(df.to_html(escape=False, index=False), unsafe_allow_html=True)
:On a working example:
It gives:
现在可以通过
LinkColumn
在streamlit
中直接使用此功能(请参阅doc 了解更多详细信息)配置。以下是如何使用它的示例:
This feature is now available directly in
streamlit
via theLinkColumn
(see doc for more details) configuration.Here is an example of how to use it: