在 jupyterlab 的 pandas df 中呈现超链接

Render hyperlink in pandas df in jupyterlab

我正在尝试在 pandas 数据帧输出中渲染 url。我跟随其他一些例子,这是我的实现:

def create_url(product_name):
    search = 'http://www.example.com/search'
    url = 'http://www.example.com/search/'+product_name
    return url

def make_clickable(url):
    return '<a target="_blank" href="{}">{}</a>'.format(url, url)

...

df['url'] = df['product_name'].apply(format_url)
df.style.format({'url': make_clickable})

这会生成格式正确的原始文本超链接,但它在输出中不可点击。

我应该补充一点,我在 AWS sagemaker jupyterlab notebook 中执行此操作,它可能会禁用输出中的超链接。不过不确定我会如何检查。

如果这不起作用,我猜这是 AWS 的问题

  1. IPython.display.HTML
  2. pandas.DataFrame.to_htmlescape=False
  3. pandas.set_option('display.max_colwidth', 2000) 必须是一个大数字才能容纳 link 标记的长度。我说我认为这会被打破。不必设置 'display.max_colwidth' 来确保 to_html 正确输出。但它是:-/

from IPython import display

pd.set_option('display.max_colwidth', 2000)

display.HTML(df.assign(url=[*map(make_clickable, df.url)]).to_html(escape=False))