title: How to get jupyter notebook port inside the notebook?
body: <p>Some applications (e.g. Bokeh) need to know the port of an application in order to work correctly. I can start a jupyter notebook with a default port, but sometimes jupyter takes a different port e.g. when the port is already in use. Therefore, I would like to get the port from inside the notebook and start my code with it, so that it runs independently. I found this snippet that does what I need, but not in every context:</p>
  
  <pre><code>%%javascript
  IPython.OutputArea.prototype._should_scroll = function(lines) {return false;}
  var nb = Jupyter.notebook;
  var port = window.location.port;
  nb.kernel.execute("PORT = '" + port + "'");
  </code></pre>
  
  <p>Together with the jupyter extension <code>AppMode</code> this code does not work anymore. Therefore, I am looking for other, more robust methods of getting the port (and URL) from inside a notebook running a python 3 kernel.</p>