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.
%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
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.