Skip to content

Instantly share code, notes, and snippets.

@laurentperrinet
Last active March 8, 2021 16:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laurentperrinet/780e73753f7abdcf73dfefdbfbb639be to your computer and use it in GitHub Desktop.
Save laurentperrinet/780e73753f7abdcf73dfefdbfbb639be to your computer and use it in GitHub Desktop.
How can I list all existing workspaces in JupyterLab?
#!/usr/bin/env python3
"""
How can I list all existing workspaces in JupyterLab?
as answered @
https://stackoverflow.com/questions/52656747/how-can-i-list-all-existing-workspaces-in-jupyterlab/53011827#53011827
"""
import argparse
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--full", action="store_true", required=False,
help="prints full path")
ap.add_argument("-u", "--url", action="store_true", required=False,
help="prints full path")
ap.add_argument("-H", "--host", required=False, help="use the given host", default='10.164.5.234')
ap.add_argument("-P", "--port", required=False, help="use the given port", default='8888')
args = vars(ap.parse_args())
import os, glob, json
for fname in glob.glob(os.path.join(os.environ['HOME'], ".jupyter/lab/workspaces/*")):
with open (fname, "r") as read_file:
id = json.load(read_file)['metadata']['id']
if args['full']:
print('id', id, '/ fname', fname)
elif args['url']:
print(f"http://{args['host']}:{args['port']}/lab/workspaces/{id}")
else:
print('id', id)
@laurentperrinet
Copy link
Author

now with full output :

$ list_workspaces.py -f
id /lab / fname /Users/lolo/.jupyter/lab/workspaces/lab-a511.jupyterlab-workspace
id /lab/workspaces/bbcp / fname /Users/lolo/.jupyter/lab/workspaces/labworkspacesbbcp-890e.jupyterlab-workspace
id /lab/workspaces/blog / fname /Users/lolo/.jupyter/lab/workspaces/labworkspacesblog-508c.jupyterlab-workspace
id /lab/workspaces/hulk / fname /Users/lolo/.jupyter/lab/workspaces/labworkspaceshulk-451c.jupyterlab-workspace
id /lab/workspaces/itwist / fname /Users/lolo/.jupyter/lab/workspaces/labworkspacesitwist-8938.jupyterlab-workspace
id /lab/workspaces/NaturalPatterns / fname /Users/lolo/.jupyter/lab/workspaces/labworkspacesnaturalpatterns-5809.jupyterlab-workspace
id /lab/workspaces/test / fname /Users/lolo/.jupyter/lab/workspaces/labworkspacestest-8c17.jupyterlab-workspace
id /lab/workspaces/TimeWarp / fname /Users/lolo/.jupyter/lab/workspaces/labworkspacestimewarp-c00e.jupyterlab-workspace

$ list_workspaces.py
id /lab
id /lab/workspaces/bbcp
id /lab/workspaces/blog
id /lab/workspaces/hulk
id /lab/workspaces/itwist
id /lab/workspaces/NaturalPatterns
id /lab/workspaces/test
id /lab/workspaces/TimeWarp

@laurentperrinet
Copy link
Author

now with URLs:

$ list_workspaces.py -u

http://localhost:8888/lab/workspaces/hulk
http://localhost:8888/lab
http://localhost:8888/lab/workspaces/blog
http://localhost:8888/lab/workspaces/test

allows right-clicking and directly opening it

@laurentperrinet
Copy link
Author

You can now specify a different host / port:


$ list_workspaces.py -u --host=127.0.0.1
http://127.0.0.1:8888/lab/workspaces/Unistellar
http://127.0.0.1:8888/lab/workspaces/hulk
http://127.0.0.1:8888/lab/workspaces/itwist
http://127.0.0.1:8888/lab/workspaces/TimeWarp
http://127.0.0.1:8888/lab/workspaces/NaturalPatterns
http://127.0.0.1:8888/lab/workspaces/MotionClouds
http://127.0.0.1:8888/lab/workspaces/laconeu
http://127.0.0.1:8888/lab/workspaces/pyavfcam
http://127.0.0.1:8888/lab/workspaces/LeCheap
http://127.0.0.1:8888/lab
http://127.0.0.1:8888/lab/workspaces/bbcp
http://127.0.0.1:8888/lab/workspaces/CODE
http://127.0.0.1:8888/lab/workspaces/blog
http://127.0.0.1:8888/lab/workspaces/test
http://127.0.0.1:8888/lab/workspaces/Where
http://127.0.0.1:8888/lab/workspaces/SDPC

$ list_workspaces.py -u --host=localhost
http://localhost:8888/lab/workspaces/Unistellar
http://localhost:8888/lab/workspaces/hulk
http://localhost:8888/lab/workspaces/itwist
http://localhost:8888/lab/workspaces/TimeWarp
http://localhost:8888/lab/workspaces/NaturalPatterns
http://localhost:8888/lab/workspaces/MotionClouds
http://localhost:8888/lab/workspaces/laconeu
http://localhost:8888/lab/workspaces/pyavfcam
http://localhost:8888/lab/workspaces/LeCheap
http://localhost:8888/lab
http://localhost:8888/lab/workspaces/bbcp
http://localhost:8888/lab/workspaces/CODE
http://localhost:8888/lab/workspaces/blog
http://localhost:8888/lab/workspaces/test
http://localhost:8888/lab/workspaces/Where
http://localhost:8888/lab/workspaces/SDPC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment