Hi all
just starting out in jupyter lab but I am THRILLED so far. I am stuck on one thing though. I am using linux mint and python3.10 and want to save a csv using the format
40_test_jlab_2023_11_26_10_23,csv.
where 40_test_jlab is the notebook name. I can easily save the file using a hardcoded value but I want to pick up, automatically, the name of the notebook in this example it would be 40_test_jlab.ipynb but I’d only use the name not the extension. I’d like to just pick it up without thinking about it.
As long as you aren’t using JupyterLite and are working in a typical, full Python kernel, you can use the package ipynbname
, see python - Get current jupyter-lab notebook name [for Jupyter-lab version 2.1 and 3.0.1 and notebook version >6.0.3) - Stack Overflow .
%pip install ipynbname
# Run the above line in a cell on its own first and the restart the kernel and make a new cell with the following as a demonstration
import ipynbname
nb_fname = ipynbname.name()
nb_path = ipynbname.path()
nb_fname
I just verified ipynbname
works in both JupyterLab 3 and 4 at present.
For your example, you can then use the following to demonstrate something along the lines of what you want:
import ipynbname
import datetime
now = datetime.datetime.now()
nb_fname = ipynbname.name()
csv_fn = f"{nb_fname}{now.strftime('_%Y_%m_%d_%H_%M')}.csv" # see http://www.saltycrane.com/blog/2008/06/how-to-get-current-date-and-time-in/ for for formatting options simplified
csv_fn
2 Likes
WOW! thank you so much @fomightez another excellent hand up for a noob. I’ll get right on it next week. Thanks so much.
Btw if you have latest versions of ipykernel/jupyter_server etc installed you can use __session__
variable to get notebook path instead (one dependency less). It will not work on older setups though.
2 Likes
@krassowski thanks so much, I’ll give it a try. I am running jupyterlab 4.0.7 and having a blast.