Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a table with clickable hyperlink in pandas & Jupyter Notebook

print('http://google.com') outputs a clickable url.

How do I get clickable URLs for pd.DataFrame(['http://google.com', 'http://duckduckgo.com']) ?

like image 365
zadrozny Avatar asked Feb 16 '17 02:02

zadrozny


People also ask

How do you make a link clickable in Python?

link() method in Python is used to create a hard link. This method creates a hard link pointing to the source named destination.

How do you create a table in pandas?

In this short guide, you'll see two different methods to create Pandas DataFrame: By typing the values in Python itself to create the DataFrame. By importing the values from a file (such as a CSV file), and then creating the DataFrame in Python based on the values imported.

How do you print a link in Python?

If you print a URL to the console, it will get underlined if you hover over it, and if you right-click on it a menu pops up, and one of the menu options is "Open link". You just print it syntactically correct. To identify the hyperlink is the job of the terminal application. – Klaus D.


2 Answers

If you want to apply URL formatting only to a single column, you can use:

data = [dict(name='Google', url='http://www.google.com'),         dict(name='Stackoverflow', url='http://stackoverflow.com')] df = pd.DataFrame(data)  def make_clickable(val):     # target _blank to open new window     return '<a target="_blank" href="{}">{}</a>'.format(val, val)  df.style.format({'url': make_clickable}) 

(PS: Unfortunately, I didn't have enough reputation to post this as a comment to @Abdou's post)

like image 125
dloeckx Avatar answered Sep 21 '22 10:09

dloeckx


Try using pd.DataFrame.style.format for this:

df = pd.DataFrame(['http://google.com', 'http://duckduckgo.com'])  def make_clickable(val):     return '<a href="{}">{}</a>'.format(val,val)  df.style.format(make_clickable) 

I hope this proves useful.

like image 31
Abdou Avatar answered Sep 19 '22 10:09

Abdou