I didn’t know the answer, but it was an interesting challenge, so I’ll step you through how I found out.
Identified root cause - the colour escape characters are being counted in the maxlen calculation, but not appearing in the printed output, so the indent is short.
Reword root cause into required fix - strip colour escape characters from the maxlen calculation
This might not be a python question but fastai in general.
I am writing the loss for object detection and it contains the location_loss and classification_loss, which will be summed up to get the final loss. I want to print out each loss after each epoch but don’t really know how. I think I can recalculate each loss in Metrics but we will have duplications.
I now declaring a global variable in loss_function for counting when I need to print the loss, as below:
How to open a jupyter notebook locally by calling a function from another notebook?
I want to use a function call like openNB(nbname) in one jupyter notebook to open another with the name nbname.
However, I have not found anything useful in google, so I put jupyter notebook command into python script with !.
#| export
def openNB(nbname):
folder ='/Users/Natsume/Documents/fastdebug/nbs/demos/'
for f in os.listdir(folder):
if f.endswith(".ipynb"):
if nbname in f:
file_name = folder + f
!jupyter notebook {file_name}
But this solution is problematic, it is not only verbose, but I can’t use the notebook anymore after calling the function above. (see the * symbol on most left of the cell)
However, in order to make the function more robust, I wonder whether I need to generate the root_server string automatically by calling another function, like get_local_jupyter_notebook_server_string()? What do you think? Thanks
Not sure if this is directly useful to you, or just broader background info…
I wanted to get the current notebook name so I could tag my kaggle submissions with this, but I struggled to get any of the answers I saw using IPython/Javascript like the following to work in JupyterLab on Paperspace…
I have too found a solution to this yesterday without using ! (because nbdev does not like ! in the source code I guess) when I try to convert current jupyter notebook into a md file by running a function inside the current notebook. I have added my source code below too.
def nb_name():
"run this func to get nb_path of this current notebook"
import ipyparams
return eval("ipyparams.notebook_name")
print(inspect.getsource(nb_path))
def nb_path():
"run this func to get nb_path of this current notebook"
import ipyparams
return eval("os.path.join(os.getcwd(), ipyparams.notebook_name)")
print(inspect.getsource(nb_url))
def nb_url():
"run this func to get nb_url of this current notebook"
import ipyparams
return eval("ipyparams.raw_url")
print(inspect.getsource(ipy2md))
def ipy2md(db=True):
"convert the current notebook to md"
import ipyparams
import os
path = nb_path()
name = nb_name()
url = nb_url()
obs_path = "/Users/Natsume/Documents/divefastai/Debuggable/jupytext"
obs_output_path = "/Users/Natsume/Documents/divefastai/Debuggable/nbconvert"
mds_path = path.replace("nbs", "mds").split(name)[0]
mds_output = "/Users/Natsume/Documents/fastdebug/mds_output"
# https://stackabuse.com/executing-shell-commands-with-python/
os.system(f"jupytext --to md {path}")
os.system(f"cp {path.split('.ipynb')[0]+'.md'} {obs_path}")
if db: print(f'cp to : {obs_path}')
os.system(f"mv {path.split('.ipynb')[0]+'.md'} {mds_path}")
if db: print(f'move to : {mds_path}')
os.system(f"jupyter nbconvert --to markdown {path}")
os.system(f"cp {path.split('.ipynb')[0]+'.md'} {mds_output}")
os.system(f"mv {path.split('.ipynb')[0]+'.md'} {mds_output}")
if db: print(f'move to : {mds_output}')
ipy2md()
[jupytext] Reading /Users/Natsume/Documents/fastdebug/nbs/2022part1/Untitled.ipynb in format ipynb
[jupytext] Writing /Users/Natsume/Documents/fastdebug/nbs/2022part1/Untitled.md
cp to : /Users/Natsume/Documents/divefastai/Debuggable/jupytext
move to : /Users/Natsume/Documents/fastdebug/mds/2022part1/
[NbConvertApp] Converting notebook /Users/Natsume/Documents/fastdebug/nbs/2022part1/Untitled.ipynb to markdown
[NbConvertApp] Writing 2700 bytes to /Users/Natsume/Documents/fastdebug/nbs/2022part1/Untitled.md
move to : /Users/Natsume/Documents/fastdebug/mds_output
(base) 10:31 ~/Documents/fastdebug > nbdev_export
Traceback (most recent call last):
File "/Users/Natsume/mambaforge/bin/nbdev_export", line 8, in <module>
sys.exit(nbdev_export())
File "/Users/Natsume/mambaforge/lib/python3.9/site-packages/fastcore/script.py", line 119, in _f
return tfunc(**merge(args, args_from_prog(func, xtra)))
File "/Users/Natsume/mambaforge/lib/python3.9/site-packages/nbdev/doclinks.py", line 135, in nbdev_export
_build_modidx()
File "/Users/Natsume/mambaforge/lib/python3.9/site-packages/nbdev/doclinks.py", line 97, in _build_modidx
res['syms'].update(_get_modidx((dest.parent/file).resolve(), code_root, nbs_path=nbs_path))
File "/Users/Natsume/mambaforge/lib/python3.9/site-packages/nbdev/doclinks.py", line 75, in _get_modidx
for tree in ast.parse(cell.code).body:
File "/Users/Natsume/mambaforge/lib/python3.9/ast.py", line 50, in parse
return compile(source, filename, mode, flags,
File "<unknown>", line 2
%load_ext autoreload
^
SyntaxError: invalid syntax
Yes, you are right. Using exec like below won’t cause error for nbdev, but the function won’t work as there is syntax error. Do you know how can I make it work?
Yes, I have tried it before but I was not sure whether it is really working. Since you mentioned this method here, I decided to try it thoroughly today and I think it really works.
Below is some detail steps I copied from stackoverlow (I forgot the url of the answer) and it works for me.
If you want it to automatically start every time you launch ipython, you need to add it to the ipython_config.py startup file:
It may be necessary to generate one first:
ipython profile create
Then include these lines in ~/.ipython/profile_default/ipython_config.py: