Streamlit 数据框中的超链接

发布于 2025-01-17 05:35:41 字数 1484 浏览 2 评论 0原文

我试图在包含 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.markdownst.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

site screenshot

I cannot use st.markdown or st.write directly as I am using st.dataframe to display the results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房敞 2025-01-24 05:35:41

您可以使用 st.write(df.to_html(escape=False, index=False), unsafe_allow_html=True)

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)

st.write(df.to_html(escape=False, index=False), unsafe_allow_html=True)

在一个工作示例中:

import pandas as pd
import streamlit as st

link1 = "https://stackoverflow.com/questions/71641666/hyperlink-in-streamlit-dataframe"
link2 = "https://stackoverflow.com/questions/71731937/how-to-plot-comparison-in-streamlit-dynamically-with-multiselect"
df = pd.DataFrame(
    {
        "url": [
            f'<a target="_blank" href="{link1}">Hyperlink in Streamlit dataframe</a>',
            f'<a target="_blank" href="{link2}">How to plot comparison in Streamlit dynamically with multiselect?</a>'
        ],
        "label": ["question", "question"]
    }
)

st.write(df.to_html(escape=False, index=False), unsafe_allow_html=True)

它给出:

SO 问题的超链接(数据框)

You can use st.write(df.to_html(escape=False, index=False), unsafe_allow_html=True):

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)

st.write(df.to_html(escape=False, index=False), unsafe_allow_html=True)

On a working example:

import pandas as pd
import streamlit as st

link1 = "https://stackoverflow.com/questions/71641666/hyperlink-in-streamlit-dataframe"
link2 = "https://stackoverflow.com/questions/71731937/how-to-plot-comparison-in-streamlit-dynamically-with-multiselect"
df = pd.DataFrame(
    {
        "url": [
            f'<a target="_blank" href="{link1}">Hyperlink in Streamlit dataframe</a>',
            f'<a target="_blank" href="{link2}">How to plot comparison in Streamlit dynamically with multiselect?</a>'
        ],
        "label": ["question", "question"]
    }
)

st.write(df.to_html(escape=False, index=False), unsafe_allow_html=True)

It gives:

Hyperlinks to SO questions (dataframe)

无所的.畏惧 2025-01-24 05:35:41

现在可以通过 LinkColumnstreamlit 中直接使用此功能(请参阅doc 了解更多详细信息)配置。

以下是如何使用它的示例:

import pandas as pd
import streamlit as st

df = pd.DataFrame(
    [
        {"name": "Google", "url": "http://google.com"},
        {
            "name": "Streamlit Link Column",
            "url": "https://docs.streamlit.io/library/api-reference/data/st.column_config/st.column_config.linkcolumn",
        },
    ]
)

st.header("DataFrame")
st.dataframe(
    df,
    column_config={"url": st.column_config.LinkColumn("URL to website")},
)

Example Screenshot

This feature is now available directly in streamlit via the LinkColumn (see doc for more details) configuration.

Here is an example of how to use it:

import pandas as pd
import streamlit as st

df = pd.DataFrame(
    [
        {"name": "Google", "url": "http://google.com"},
        {
            "name": "Streamlit Link Column",
            "url": "https://docs.streamlit.io/library/api-reference/data/st.column_config/st.column_config.linkcolumn",
        },
    ]
)

st.header("DataFrame")
st.dataframe(
    df,
    column_config={"url": st.column_config.LinkColumn("URL to website")},
)

Example Screenshot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文