Bananach
Bananach

Reputation: 2311

Jupyter notebook: Display dataframe such that clicking cells opens files in file explorer (e.g. Windows explorer)

Is there a way to display a dataframe in a Jupyter notebook such that clicking cells opens a Windows explorer (or any other native explorer) in a given directory?

I tried

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

df = pd.DataFrame({'dir': '.'}, index=[0])
styled = df.style.format({'dir': make_clickable})
display(styled)

df = pd.DataFrame({'dir': 'file:///C:/'}, index=[0])
styled = df.style.format({'dir': make_clickable})
display(styled)

The former opens a new browser tab in which the Jupyter tree is displayed (at the correct relative location, if I replace . by another relative path).

The latter doesn't do anything. If I right-click open in a new tab, it gives about blank#blocked and if I copy and paste it in a new tab address line, it shows the browser built in explorer, not the Windows explorer.

I tried replacing _blank by _explorer.exe but that doesn't change anything for either variant.

Thus, both options are far from what I want.

(I know, security... but, this is for an in-house tool in a a completely off-line setting)

I know it is somehow possible to open explorers from the browser, because I can just use os.startfile in a Jupyter cell, so I was thinking maybe there is a way to execute arbitrary code when cells of a dataframe are clicked? Maybe this requires some sort of widget?

I could always write an entire Python GUI of course, but I'd really rather not.

Upvotes: 0

Views: 327

Answers (2)

Equinox
Equinox

Reputation: 6748

I am a bit late but I finally figured it out. Javascript itself is unable to open file explorer for security reasons. However javascript can execute python code inside Jupyter notebook(Jupyter.notebook.kernel.execute) and python can open windows explorer using(os.startfile('.'),subprocess.run etc.).

Below is the code. The linux one works fine as intended but with windows the explorer opens but doesn't move into user focus it also shows the orange beeping light behind the icon show it should be fine.

import pandas as pd
import os
import subprocess

windowsv1 = """ 
<p onclick="Jupyter.notebook.kernel.execute(`os.startfile('C:\\Users')`)">File Explorer.</p>
"""
windowsv2 = """ 
<p onclick="Jupyter.notebook.kernel.execute(`subprocess.run(['explorer', 'C:\\Users'])`)">File Explorer.</p>
"""

linux = """ 
<p onclick="Jupyter.notebook.kernel.execute(`subprocess.call([opener, r'/home/equinox'])`)">File Explorer.</p>
"""

def make_clickable(url):
    return windowsv2

df = pd.DataFrame({'dir': '.'}, index=[0])
styled = df.style.format({'dir': make_clickable})
display(styled)

Upvotes: 1

Dominik Sajovic
Dominik Sajovic

Reputation: 841

I am not 100% on this but I think this will depend on the browser you are using. Furthermore, I think most browsers will block this kind of behavior and not allow a web page link to open your file browser.

Please, someone, correct me if I'm wrong.

EDIT 2020-08-15

It is possible to do it if you install a Chrome Browser extension. Just follow the below link. After your code worked for me.

https://www.alphr.com/browse-and-open-folders-files-chrome/

  1. Scroll down to Title "Open Any Local File From Chrome".
  2. Follow all the steps
  3. Don't forget to restart Chrome after installing the browser extension (and the extra installation file)

Upvotes: 1

Related Questions