Hyperlink in Streamlit dataframe

2024/11/17 18:29:43

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, HTMLdf = 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 linktext = 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.

Answer

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, HTMLdf = 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 linktext = 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 stlink1 = "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)

https://en.xdnf.cn/q/71154.html

Related Q&A

Best way to detect if checkbox is ticked

My work:Scan the paper Check horizontal and vertical line Detect checkbox How to know checkbox is ticked or notAt this point, I thought I could find it by using Hierarchical and Contours: Below is my w…

ResultSet object has no attribute find_all

i always met one problem, when I scraping one web page.AttributeError: ResultSet object has no attribute find. Youre probably treating a list of items like a single item. Did you call find_all() when y…

How can I set path to load data from CSV file into PostgreSQL database in Docker container?

I would like to load data from CSV file into PostgreSQL database in Docker. I run:docker exec -ti my project_db_1 psql -U postgresThen I select my database:\c myDatabaseNow I try to load data from myfi…

Why is this simple Spark program not utlizing multiple cores?

So, Im running this simple program on a 16 core multicore system. I run it by issuing the following.spark-submit --master local[*] pi.pyAnd the code of that program is the following. #"""…

How to get python dictionaries into a pandas time series dataframe where key is date object

I have a python dictionaries where the key is a dateobject and the value is the timeseires.timeseries = {datetime.datetime(2013, 3, 17, 18, 19): {t2: 400, t1: 1000},datetime.datetime(2013, 3, 17, 18, 2…

Changing the color of an image based on RGB value

Situation:You have an image with 1 main color and you need to convert it to another based on a given rgb value.Problem:There are a number of different, but similar shades of that color that also need …

Python NumPy - FFT and Inverse FFT?

Ive been working with FFT, and Im currently trying to get a sound waveform from a file with FFT, (modify it eventually), but then output that modified waveform back to a file. Ive gotten the FFT of the…

Tools to help developers reading class hierarchy faster

I mostly spend time on Python/Django and Objective-C/CocoaTouch and js/jQuery in the course of my daily work.My editor of choice is vim for Python/Django and js/jQuery and xcode for Objective-C/CocoaTo…

Python Last Iteration in For Loop [duplicate]

This question already has answers here:What is the pythonic way to detect the last element in a for loop?(34 answers)How do I read and write CSV files?(7 answers)Closed 9 months ago.Is there any simp…

Django 1.7 multisite User model

I want to serve a Django application that serves multiple web sites by single database but different user sets. Think like a blog application, it will be used by several domains with different themes, …