2

It is possible to assign the google colab notebook name to a python variable. In Jupyter you can use javascript but this does not work in colab. I have found examples using %javascript to access the html but these do not appear to work with colab notebooks. So I want to copy the notebook to my google drive and rename it but then assign the new name to a python variable.

2 Answers 2

6

Here's the code to get the notebook name.

from requests import get
filename = get('http://172.28.0.2:9000/api/sessions').json()[0]['name']

UPDATE - Jan 2023

The URL has changed from "172.28.0.2" to "172.28.0.12". See the other post for details.

New code:

from requests import get
filename = get("http://172.28.0.12:9000/api/sessions").json()[0]["name"]

Or, to adapt if ip is changed again

from requests import get
from socket import gethostname, gethostbyname
ip = gethostbyname(gethostname()) # 172.28.0.12
filename = get(f"http://{ip}:9000/api/sessions").json()[0]["name"]
Sign up to request clarification or add additional context in comments.

1 Comment

This workaround doesn't work anymore.
2

As of Jan 2023, korakot's solution needs to be updated to the new URL from "172.28.0.2" to "172.28.0.12". The working code is below:

import requests
filename = requests.get("http://172.28.0.12:9000/api/sessions").json()[0]["name"]
# Output will be of the form "xyz.ipynb"

Alternative: A solution robust to IP changes

colab_ip = %system hostname -I   # uses colab magic to get list from bash
colab_ip = colab_ip[0].strip()   # returns "172.28.0.12"
colab_port = 9000                # could use 6000, 8080, or 9000

import requests
filename = requests.get(f"http://{colab_ip}:{colab_port}/api/sessions").json()[0]["name"]
# Output will be of the form "xyz.ipynb"

Explanation

In case the URL changes again the future, you can find the latest IP address using code from this post: https://stackoverflow.com/a/66709565/2879686. The relevant code is below:

# Gives IP address
!hostname -I
print()  # empty line
# Gives IP addresses with port numbers
!sudo lsof -i -P -n | grep LISTEN

The current output for me is:

172.28.0.12 

node        7 root   21u  IPv6  19966      0t0  TCP *:8080 (LISTEN)
kernel_ma  35 root    7u  IPv4  19643      0t0  TCP 172.28.0.12:6000 (LISTEN)
colab-fil  65 root    3u  IPv4  19927      0t0  TCP *:3453 (LISTEN)
colab-fil  65 root    4u  IPv6  19929      0t0  TCP *:3453 (LISTEN)
jupyter-n  91 root    4u  IPv4  20144      0t0  TCP 172.28.0.12:9000 (LISTEN)
python3   138 root   23u  IPv4  20719      0t0  TCP 127.0.0.1:45899 (LISTEN)
python3   174 root    3u  IPv4  19440      0t0  TCP 127.0.0.1:18901 (LISTEN)
python3   174 root    5u  IPv4  20935      0t0  TCP 127.0.0.1:40271 (LISTEN)

Interestingly, there are multiple ports that work:

  • 172.28.0.12:6000
  • 172.28.0.12:8080
  • 172.28.0.12:9000

You can use colab magic to create a solution that is robust to IP changes.
Reference: https://ipython.readthedocs.io/en/stable/interactive/magics.html#cell-magics

colab_ip = %system hostname -I   # uses colab magic to get list from bash
colab_ip = colab_ip[0].strip()   # returns "172.28.0.12"
colab_port = 9000                # could use 6000, 8080, or 9000

import requests
filename = requests.get(f"http://{colab_ip}:{colab_port}/api/sessions").json()[0]["name"]
# Output will be of the form "xyz.ipynb"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.