Can not open file 'notebook2script.py' in part 2 lesson 1 with colab

Hi,
I run the 00_exports.ipynb in my colab and the code is :slight_smile: image
what should I do to find the file named ‘notebool2script.py’
Thanks

Hi Shengrehou

Strangely I posted this last night.

I am probably reinventing the wheel here.

Goto Colab
Select lesson 8 - https://colab.research.google.com/github/fastai/course-v3/blob/master/nbs/dl2/00_exports.ipynb
Save to drive. It becomes Copy of 00_exports.ipynb. This is saved to folder Colab Notebooks on your Google Drive.
Rename it to 00_exports.ipynb (File->Rename)

On a new tab locate

github.com

fastai/course-v3/blob/master/nbs/dl2/notebook2script.py

#!/usr/bin/env python

import json,fire,re
from pathlib import Path
import io

def is_export(cell):
    if cell['cell_type'] != 'code': return False
    src = cell['source']
    if len(src) == 0 or len(src[0]) < 7: return False
    #import pdb; pdb.set_trace()
    return re.match(r'^\s*#\s*export\s*

This file has been truncated. show original

Next retrieve the source code and select the RAW option to get the text. Save this to your computer and upload it to the folder Colab Notebooks on your Google Drive

Return to Colab and make Colab Netbooks the working directory

!curl -s https://course.fast.ai/setup/colab | bash
!pip install fire
from google.colab import drive
drive.mount(’/content/gdrive’, force_remount=True)
%cd “/content/gdrive/My Drive/Colab Notebooks/”

Now you can continue the lesson

#export
TEST = ‘test’

!python notebook2script.py 00_exports.ipynb

import json
d = json.load(open(‘00_exports.ipynb’,‘r’))[‘cells’], src[0], re.IGNORECASE) is not None

def getSortedFiles(allFiles, upTo=None):
‘’'Returns all the notebok files sorted by name.
allFiles = True : returns all files
= ‘_.ipynb’ : returns this pattern
upTo = None : no upper limit
= filter : returns all files up to ‘filter’ included
The sorting optioj is important to ensure that the notebok are executed in correct order.


This file has been truncated. [show original](https://github.com/fastai/course-v3/blob/master/nbs/dl2/notebook2script.py)

Next retrieve the source code and select the RAW option to get the text. Save this to your computer and upload it to the folder Colab Notebooks on your Google Drive

Return to Colab and make Colab Netbooks the working directory

!curl -s https://course.fast.ai/setup/colab | bash
!pip install fire
from google.colab import drive
drive.mount(’/content/gdrive’, force_remount=True)
%cd “/content/gdrive/My Drive/Colab Notebooks/”

Now you can continue the lesson

#export
TEST = ‘test’

!python notebook2script.py 00_exports.ipynb

import json
d = json.load(open(‘00_exports.ipynb’,‘r’))[‘cells’]
3 Likes

Hi Shengrehou

In !./notebook2script.py 02_fully_connected.ipynb I encounter two problems.
When uploading prepare the file with notepad++ to convert windows crlf to unix lf.
Also !chmod 777 notebook2script.py 02_fully_connected.ipynb

I do not know why it worked in the 00_export but these fixes resolves the problem.

Regards Conwyn

1 Like

Hi, Conwyn:
Thanks for your reply. According to your suggestion, I solved the problem.

Hi Shengrenhou

In 06_cuda_cnn_hooks_init.ipynb

I found this comment about exporting

Here’s a handy way to export our module without needing to update the file name -
after we define this, we can just use nb_auto_export() in the future (h/t Stas Bekman):

#export
from IPython.display import display, Javascript
def nb_auto_export():
display(Javascript("""{
const ip = IPython.notebook
if (ip) {
ip.save_notebook()
console.log(‘a’)
const s = !python notebook2script.py ${ip.notebook_name}
if (ip.kernel) { ip.kernel.execute(s) }
}
}"""))

nb_auto_export()

I could not make this work when using colab. The solution comes from Korakot in Thailand from Stack Overflow.

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

notebookname contains ‘06_cuda_cnn_hooks_init.ipynb’
so you can run

!python notebook2script.py {notebookname}

Regards Conwyn