title: Induce IPython notebook output cell programmatically from a different source file
body: <p>Suppose there is a Python file <code>myfile.py</code>. The contents of the file can be treated as if it was pasted into a single cell in an IPython notebook and then executed (and any output generated should be generated in the notebook).</p>
  
  <p>Is it possible to programmatically cause this to happen? That is, I want to invoke something like "python myfile.py" and have the result be that a notebook is launched, a tab pops open, and <em>only the output that would have resulted from myfile.py</em> is displayed already in the notebook.</p>
  
  <p>I'm specifically looking to avoid the case where the .py file is "turned into a notebook" and the code-to-be-executed appears as an input cell.</p>
  
  <p>The idea is for invocation of this .py file to function like a web app -- so just the output should pop up in a notebook. The notebook's kernel should still be operating though, in case there is interactivity that must post callbacks to the code from the .py file.</p>
  
  <p>Each piece seems to exist in IPython already (interactive widgets, conversion to notebook, even just using Popen to launch the notebook) -- but it's unclear how to launch from a plain .py file in such a way that only the output appears.</p>
  
  <p>Here is an attempt:</p>
  
  <pre><code>import sys
  import subprocess
  from IPython.nbformat import current as nbf
  
  # Parse the output cell needed from the file
  py_file_name = sys.argv[1] 
  with open(py_file_name, 'r') as py_file:
      py_code = py_file.read()
  
  # Set an input cell and invoke it.
  nb = nbf.new_notebook()
  cells = [nbf.new_code_cell(py_code)]
  nb['worksheets'].append(nbf.new_worksheet(cells=cells))
  
  # Store the result into a notebook.
  notebook_name = py_file_name + '.ipynb'
  with open(notebook_name, 'w') as f:
      nbf.write(nb, f, 'ipynb')
  
  # Launch a new IPython notebook kernel
  subprocess.call(["ipython", "-c", "'%run {}'".format(notebook_name)])
  </code></pre>
  
  <p>This isn't ideal -- since it still requires me to populate the <em>input</em> part of a cell, and then to actually open the file and execute it.</p>
  
  <p>Even so, I get an error when I use the <code>%run</code> option at the bottom, like this:</p>
  
  <pre><code>ely@eschaton:~$ python launcher.py myfile1.py
  ---------------------------------------------------------------------------
  NameError                                 Traceback (most recent call last)
  /home/ely/anaconda/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
      202             else:
      203                 filename = fname
  --&gt; 204             __builtin__.execfile(filename, *where)
  
  /home/ely/myfile1.py.ipynb in &lt;module&gt;()
        8     {
        9      "cell_type": "code",
  ---&gt; 10      "collapsed": false,
       11      "input": [
       12       "import numpy as np\n",
  
  NameError: name 'false' is not defined
  </code></pre>