0

I open my Python scripts on JupyterLab and make some changes if required. And I need to save changes using written command in my script. Something that will immitate CTRL+S command and do update my local file with all the changes.

Does anybody know how to handle this situation?

  • Welcome to Stack Overflow. Please edit the question to include a [*"Minimal, Reproducible, Example."*](https://stackoverflow.com/help/minimal-reproducible-example) – bad_coder Feb 21 '22 at 14:35

2 Answers2

1

There are some python modules to handle this situation for you(like keyboard and PyAutoGUI ). but they are not built-in python and first of all you have to install them using pip install command(you have to have admin privilege to install them). but if you want to save your python script using a command in Jupyter, you can use the following command in a jupyter cell and press Enter:

%%javascript
IPython.notebook.save_notebook()

It will automatically save your changes.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
MHM
  • 194
  • 1
  • 9
  • It gives an error: ```Javascript Error: IPython is not defined.``` Any ideas? – Zaur Huseynov Feb 20 '22 at 09:06
  • I just search this error and found this result for you:[link](https://stackoverflow.com/questions/51922480/javascript-error-ipython-is-not-defined-in-jupyterlab) I hope it could solve your problem. – MHM Feb 20 '22 at 09:17
  • Probably this solutions is OK for ```%matplotlib widget```. Unfortuantely it does not help with ```%%javascript```. :( – Zaur Huseynov Feb 20 '22 at 09:22
0

You can use keyboard module.

import keyboard # Do pip install keyboard if it shows ModuleNotFoundError
#Your code
keyboard.press_and_release('ctrl + s') #Saves the code
Abhyuday Vaish
  • 2,357
  • 5
  • 11
  • 27