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.
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)