Issue
I'm automating a bunch of tasks locally with python and shell scripts. I've created a python socket server and clients so I can automate the execution of scripts in different shells and envs. It's not fully-functional, but I can fire up shells, run scripts, and have them connect to the server before executing. One app uses jupyter lab and notebooks. I'm not finding much about how to automate this for what I need.
I tried converting my ipynb
file to a py
file thinking I could treat it as an ordinary python script, then use a watch folder, load the file names in a dict, and just script in some loops with variable file names, but that doesn't seem to be the case—at least I haven't found anything discussing how to do it with notebooks.
This is what I need to accomplish (programmatically):
- Open a new shell, activate env and launch jupyter lab (this I can do)
- Connect jupyter lab to the socket server (guessing I can do this in a cell?)
- Create a cell with a command that loops through each file in a specific folder with args
- Execute all cells in the notebook
- Send a message back to socket server that things have completed
Whether or not I need to convert the ipynb
to a py
file or automate jupyter lab doesn't matter to me, as long as I can automate it. Point in the right direction would be appreciated.
Solution
Being new to jupyter notebooks, I wasn't understanding properly how they work. I didn't realize there was a flag when converting to completely strip out all the data except for the raw code. --no-prompt
Running this command will completely convert the ipynb
file to the raw code py
jupyter nbconvert --to script --no-prompt myfile.ipynb
Once you do that, they are just like any other python script. I was able to easily add in all my code and have the script working as needed now.
That being said, the suggested tools by @krassowski would be fantastic for more complex projects in the future, especially in a case when you don't want to convert the notebooks.
Answered By - liquidRock
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.