I know that for jupyter notebooks and jupyter lab, there are available code formatter extensions such as nb_black
or blackcellmagic
. However when I installed them, it doesn't seem to work on google colab.
Do you know if there are any native option in colab or an extension that formats code (pep8 compliant)?
Kaggle. Kaggle is another Google product with similar functionalities to Colab. Like Colab, Kaggle provides free browser-based Jupyter Notebooks and GPUs. Kaggle also comes with many Python packages preinstalled, lowering the barrier to entry for some users.
All Colab notebooks are stored in the open source Jupyter notebook format ( . ipynb).
Colab interfaceFrom the tabs at the top of the file explorer, select a source and navigate to the . ipynb file you wish to open. The file explorer can also be accessed from the Colab interface by selecting File > Open notebook or using the Ctrl+O keyboard combination.
I don't think there's an extension directly in Colab.
What you could do, though, is to download your notebook, run
pip install -U nbqa
nbqa black notebook.ipynb
and then reupload your (now formatted) notebook to Colab
disclaimer: I'm the author of nbQA
UPDATE: as of version 21.8b0, black
runs directly on notebooks, no third-party tool required
I have tried everything, none of the JupyterLab/Notebook backend hack methods seem to work as of February 2022. However, until later here is a relatively simple workaround:
[Run only once, at startup]
Connect to your drive
from google.colab import drive
drive.mount("/content/drive")
Install black
for jupyter
!pip install black[jupyter]
Restart kernel
[Then]
.ipynb
file somewhere on your drive!black /content/drive/MyDrive/YOUR_PATH/YOUR_NOTEBOOK.ipynb
F5
to refresh the pageIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With