title: Get the current contents of the entire Jupyter Notebook
body: <p>I have a Jupyter Notebook running. I want to be able to access the source of the current Jupyter Notebook from within Python. My end goal is to pass it into <code>ast.parse</code> so I can do some analysis on the user's code. Ideally, I'd be able to do something like this:</p>
  
  <pre><code>import ast
  ast.parse(get_notebooks_code())
  </code></pre>
  
  <p>Obviously, if the source code was an IPYNB file, there'd be an intermediary step of extracting the code from the Python cells, but that's a relatively easy problem to solve.</p>
  
  <p>So far, I've found code that will use the <code>list_running_servers</code> function of the IPython object in order to make a request and match up kernel IDs - this gives me the filename of the currently running notebook. This would work, except for the fact that the source code on disk may not match up with what the user has in the browser (until you save a new checkpoint).</p>
  
  <p>I've seen some ideas involving extracting out data using JavaScript, but that requires either a separate cell with magic or calling the display.Javascript function - which fires asynchronously, and therefore doesn't allow me to pass the result to <code>ast.parse</code>.</p>
  
  <p>Anyone have any clever ideas for how to dynamically get the current notebooks source code available as a string in Python for immediate processing? I'm perfectly fine if I need to make this be an extension or even a kernel wrapper, I just need to get the source code somehow.</p>