In general, the python (kernel) does not know if it is running code from a notebook, console, or some other service. The architecture is specifically designed to be agnostic that way to give you great power and flexibility in how to run code. For example, it's easy to run one cell from a notebook, open up a console to the same kernel, and run the next few bits of code from the console as you investigate the environment interactively outside of the notebook. As such, I don't know of any way to tell from a given piece of code being run that it is (a) sent from a notebook, and (b) get the metadata from the notebook.
Of course, if you are just talking about using the python kernel to open up a notebook file itself, you can retrieve the metadata by just reading in the notebook JSON.