posit-dev/py-shiny

How to render a clickable link inside `ui.output_data_frame`

Opened this issue · 0 comments

I would like to include a clickable link in the cell of ui.output_data_frame. I have tried a few iterations but cannot find anything that works. This question is similar to #384.

  1. Raw text
import pandas as pd
from shiny import App, render, ui


app_ui = ui.page_fluid(
    ui.output_data_frame("data_output")
)


def server(input, output, session):
    # Load the data
    data = pd.DataFrame({
        "id": [1, 2, 3],
        "url": ['https://google.com', 'https://duckduckgo.com', 'https://bing.com']
    })

    @output
    @render.data_frame
    def data_output():
        df = data.copy()
        return df


app = App(app_ui, server)

Screenshot 2023-12-20 at 15 14 48@2x

  1. Use ui.a
    @output
    @render.data_frame
    def data_output():
        df = data.copy()
        df['url'] = df['url'].apply(lambda x: ui.a(x))
        return df

Screenshot 2023-12-20 at 15 16 28@2x

  1. Raw HTML with <a href=""></a>
    @output
    @render.data_frame
    def data_output():
        df = data.copy()
        df['url'] = df['url'].apply(lambda x: f'<a href"{x}">click me</a>')
        return df

Screenshot 2023-12-20 at 15 17 31@2x

  1. Trying suggestions from #384
    @output
    @render.data_frame
    def data_output():
        df = data.copy()
        df['url'] = df['url'].apply(lambda x: f'<a href"{x}"><div>click me</div></a>')
        return df

Screenshot 2023-12-20 at 15 22 49@2x