With this extension, each Jupyter notebook receives an UUID, which is stored in the metadata of the notebook. Every time the notebook is loaded or the kernel restarted, the UUID is passed to the kernel namespace, where you can access it with '_notebook_id_'. This does currently only work with Python!
Clone this repository and run
jupyter nbextension install notebook_id
jupyter nbextension enable notebook_id/main