Reading a string to read IPython from a text cell

I have a source text cell in my IPython notebook project.

Is there a way to get the text as a string with an inline function or something similar?

+3


source to share


2 answers


My (possibly unsatisfactory) answer has two parts. This is based on personal research on iPython structures, and it is possible that I missed something that answers the question more directly.

Current session

The raw text for code cells entered during the current session is available in the notebook using a list In

.

Thus, the original text of the current cell can be returned by the following expression inside the cell:

In[len(In)-1]

      

For example, evaluating a cell containing this code:

print "hello world"
three = 1+2
In[len(In)-1]

      

gives the corresponding value Out[]

:

u'print "hello world"\nthree = 1+2\nIn[len(In)-1]'

      

So, in an active laptop session, you can access the original text of a cell as In[n]

, where n

is the displayed index of the required cell.

But if a cell was entered during a previous Notebook session that was subsequently closed and reopened, this no longer works. In addition, In

only code cells are included in the array .

Also, it doesn't work for non-code cells, so it won't work for raw text cells.

Cells from Saved Notebook Sessions

In my research, the only way I could open the source from previous sessions is by reading the laptop source file. There is a documentation page Importing IPython Notebooks as Modules describing how to do this. key code is in In[4]

:



    # load the notebook object
    with io.open(path, 'r', encoding='utf-8') as f:
        nb = current.read(f, 'json')

      

where current

is the API instance described in Module: nbformat.current .

The returned notebook object is accessed as a nested dictionary and list structure, for example:

    for cell in nb.worksheets[0].cells:
        ...

      

Objects cell

listed this way have two key fields for this question:

  • cell.cell_type

    - cell type ("code", "markdown", "raw", etc.).

  • cell.input

    is the raw text content of the cell as a list of lines, with an entry for each line of text.

A lot of this can be seen by looking at the JSON data, which is the saved iPython notebook.

Apart from the "prompt number" fields in the notebook, which seem to change whenever the field is reevaluated, I couldn't find any way to create a stable reference to a notebook cell.

Conclusion

I couldn't find an easy answer to the original question. What I found is described above. Not knowing the motivation for the original question, I don't know if this is enough.

What I was looking for, but could not identify, was a way to reference the current laptop that can be used from within the laptop itself (using a type function, for example get_ipython()

). This does not mean that it does not exist.

Another missing element in my answer is any stable way to reference a specific cell. (For example, looking at the notebook file format, source text cells are solely composed of the cell type ("raw") and the source text itself, although it seems that cell metadata could also be included.) This suggests that the only way to directly reference the cell is across its place in the notebook, but this changes too much when the notebook is edited.

(Researched and answered as part of Oxford's involvement at http://aaronswartzhackathon.org )

+2


source


I am not allowed to comment due to my lack of reputation, so I will just post an answer on how to answer Graham Cline's question in case anyone else stumbles upon it. (Ipython doesn't have updated documentation yet)

  • Use nbformat instead of Ipython.nbformat.current
  • Worksheets attribute is not used, so use cells directly.


I have an example of what the updated code would look like: https://github.com/ldiary/marigoso/blob/master/marigoso/NotebookImport.py

+1


source







All Articles