How to run a script when a notebook is loaded - python

I am writing a set of notebooks that has some common start up script. I want to put this startup script(a somefile.py file) in the notebook folder, and when the notebook's kernel is started(or restarted), I want this script run in the kernel.
I know that I can put this file in the profile folder, but since I am working on the set of notebooks on a few PC, and I only want the script been run for the set of notebooks. A global startup script is not very good for this case.

Related

Execute a python script (.py) located inside a hidden folder from a .ipynb file on Jupyter Notebook

I have a python script located inside a hidden folder due to security reasons inside a Jupyter Notebook environment (JupyterHub/JupyterLab). However I want to execute it from the notebook cell.
I have come across this post How to execute a * .PY file from a * .IPYNB file on the Jupyter notebook?
And tried the same with the magic function
%run -i 'script.py'
But it says No file or directory found. Is there a way to execute a hidden python file from the code cell?
You still need to use the path to the file.
Assuming the hiding is the only security measure, you can still use %run. Let's suppose your script my_script.py is in a hidden directory named hidden_dir that occurs in the same directory as your notebook file. This is your command:
%run './.hidden_dir/my_script.py'
Remember, when working out the path for your %run statement, you can always use ls in cells in the notebook to test and see what it can actually see and access. In this case, running the following in a cell in the notebook should list the script my_script.py:
ls .hidden_dir -lah
You'd just use ls -lah in a notebook cell in the first place to see the directories, including hidden ones. Compare the results of that to what you see with ls -lh because with ls -lh the hidden directories won't be shown without the -a option.
Note, unless you want to run the script in the notebook namespace, it is just %run 'path/to/script/<name_of_script>'. You didn't say if you needed that option or not, and so I left it out in my example to further simplify. Add in the -i option if you are setting some options in the notebook or want objects created in the script accessible in the notebook after running.
Related to sorting out the path, there's a common 'gotcha' involved with using cd in a cell. If you need to change the working directory within the namespace of the notebook itself, you use the %cd magic (documented here), and not !cd or equivalent. !cd or equivalent only changes the directory off in the shell instance that gets triggered by the exclamation point. Once that line of code is done, the temporary shell is gone, and so you've done nothing to the notebook. You can always check the current working directory in the notebook by running the following in a notebook cell:
pwd
Together, ls (and the variant ls -lah), %cd, & pwd let you explore what the Jupyter notebook has access to and work out paths for use in Python and shell commands in the Jupyter notebook.

How to save a default python env for VSCode Jupyter Notebooks

I've started playing around with the new VSCode notebooks feature and I find one aspect of it particularly annoying.
I open my "jupyter_notebooks" directory the same way I would when using a typical jupyter server, except I open it in VSCode. And I click on a script to bring it into the editor.
However, when I go to run my first cell, it asks me which python environment I want to run it in. This happens every time I start running any script... this is particularly annoying because the "suggested" environment isn't the one that I always use.
Is there a way to set the default python env to a vscode workspace or something so that when I open that folder in vscode it just assumes which env I want to use and stops asking me?
This is an image of the menu that keeps popping up every time I want to run a new script.
You can use "Ctrl+Shift+P" and choose "Python:Select Interpreter"
Then reload window can work.

How to config automatic sync Jupyter notebook .ipynb and .py files in VSCode e.g. by using Jupytext

I have written some Jupyter notebooks using original Jupyter notebook web interface. All notebooks are synced nicely in this way.
But now, I would like to edit my notebooks in the VSCode. But I cannot configure syncing notebook file with its python script.
I tried this using jupytext:
created file jupytext in the folder ~/.config
put the next code into this file:
# Always pair ipynb notebooks to py:percent files
default_jupytext_formats = "ipynb,py:percent"
But no effect!
(Update) Can this be achieved, as a first solution, using VSCode Tasks (I am not used tasks yet)?
May be it possible to run the task with jupytext command if the notebook file is opened/saved/modified?
Currently, VSCode does not support such a function. The Jupyter function in VSCode is provided by a Python extension, which supports us to convert between .ipynb files and .py files in VSCode.
.ipynb files to .py files : Export as python script.
.py files to .ipynb files : Right click, "Export Current Python File as Jupyter Notebook"
I have submitted the requirement you described, and we look forward to the realization of this feature. Giuhub link: How to synchronize the jupyter file and python file of VSCode.

Running all script in a directory python

If I have 5 python scripts and they are all in one directory how would I go about running all of them? I dont want to have to go and manually open them and then run them I would like to have python do this.
So i have a folder named Scripts and inside it has five scripts that all have different names with the .ipynb extension.
so the breakdown of the Tree looks like this,
-Scripts
-A.ipynb
-B.ipynb
-C.ipynb
-D.ipynb
-E.ipynb
How could I run each of the files in this directory without manually opening them and running them each individually?
The extension 'ipynb' isn't a python script, but, you can create a batch file which runs all of your .py files.
Example:
#ECHO OFF
Runs my Python Scripts
C:\path-to-python\Python.exe C:\path-to-scripts\A.py
ECHO Ran script A
(Repeat as needed)
PAUSE

Open a Jupyter notebook within running server from command line

My Jupyter/IPython notebooks reside in various directories all over my file system. I don't enjoy navigating hierarchies of directories in the Jupyter notebook browser every time I have to open a notebook. In absence of the (still) missing feature allowing to bookmark directories within Jupyter, I want to explore if I can open a notebook from the command line such that it is opened by the Jupyter instance that is already running. I don't know how to do this....
Option 1: Run multiple jupyter notebook servers from your project directory root(s). This avoids navigating deeply nested structures using the browser ui. I often run many notebook servers simultaneously without issue.
$ cd path/to/project/; jupyter notebook;
Option 2: If you know the path you could use webbrowser module
$ python -m webbrowser http://localhost:port/path/to/notebook/notebook-name.ipynb
Of course you could alias frequently accessed notebooks to something nice as well.

Categories

Resources