pandas Streamlit Dataframe 中的超链接

iklwldmw  于 2023-09-29  发布在  其他
关注(0)|答案(2)|浏览(183)

我试图在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来显示结果。

92dk7w1h

92dk7w1h1#

可以使用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)

它给出:

qybjjes1

qybjjes12#

此功能现在可通过LinkColumn(有关详细信息,请参阅doc)配置直接在streamlit中使用。
下面是一个如何使用它的例子:

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")},
)

相关问题