googlecolab / colabtools

Python libraries for Google Colaboratory
Apache License 2.0
2.2k stars 721 forks source link

google.colab: Proper way to install and enable extension for local use? #1415

Closed EricCousineau-TRI closed 4 years ago

EricCousineau-TRI commented 4 years ago

Describe the current behavior

On my system (Python 3.6, Ubuntu 18.04), please see this gist: https://gist.github.com/EricCousineau-TRI/18a67c8712f7c96218fd48a63b900217#file-google-colab_nbextension_attempt-txt

Final lines:

+ jupyter nbextension enable --py google.colab --user
Traceback (most recent call last):
  File "${parent_dir}/colabtools/venv/bin/jupyter-nbextension", line 8, in <module>
    sys.exit(main())
...
  File "${parent_dir}/colabtools/venv/lib/python3.6/site-packages/notebook/nbextensions.py", line 386, in <listcomp>
    for nbext in nbexts]
KeyError: 'require'

Relates:

Describe the expected behavior:

It works?

Possible solutions:

The web browser you are using (Chrome, Firefox, Safari, etc.):

N/A

Link (not screenshot!) to a minimal, public, self-contained notebook that reproduces this issue (click the Share button, then Get Shareable Link):

N/A. See Gist above for reproduction.

craigcitro commented 4 years ago

Unfortunately, this package isn't intended for local use at all.

EricCousineau-TRI commented 4 years ago

Makes sense.

FWIW, tried a few permutations on google.colab/__init__.py, and could only get partway to allowing the extension to be enabled:

diff --git a/google/colab/__init__.py b/google/colab/__init__.py
index 7761954..0ee93bb 100644
--- a/google/colab/__init__.py
+++ b/google/colab/__init__.py
@@ -45,6 +45,7 @@ def _jupyter_nbextension_paths():
       'dest': 'google.colab',
       'section': 'notebook',
       'src': 'resources',
+      'require': 'google.colab/tabbar_main.min',
   }]
matteoferla commented 2 years ago

Just passing by and wanted to drop a snippet: For anyone hoping to run a Colab notebook in Jupyter and not wishing to edit out the from google.colab import lines and not intending to use the colab bits then this snippet does the trick:

import sys
import types
from unittest.mock import Mock

fauxgle = types.ModuleType("google.colab")
fauxgle.__dict__.update(  dict(files=Mock(), auth=Mock(), drive=Mock())  )
sys.modules["google.colab"] = fauxgle

from google.colab import files, auth, drive

With a mock you can do mock.foo().bar, but no operations (e.g. mock / 10 ), subscriptions (e.g. mock['foo']) and majorly iterate it as may happen with for fn in mock_files.upload().