How to get the text content of a code cell?

1,451 views
Skip to first unread message

Zelphir Kaltstahl

unread,
Mar 14, 2017, 6:20:18 AM3/14/17
to Project Jupyter
So as far as I know Jupyter Notebook uses the CodeMirror editor to render code cells. In the CodeMirror documentation I found the method `doc.getValue` which is supposed to give the editor's content. According to this topic, there is no documentation for the JS API of Jupyter Notebook.

My question is: How do I get the (code) content of a/all code cell(s)? Better even would be a way to get only the code cell's contents of code cells, which are assigned a specific CSS class or other identifying feature.

Kiko

unread,
Mar 14, 2017, 7:20:55 AM3/14/17
to jup...@googlegroups.com
2017-03-14 11:20 GMT+01:00 Zelphir Kaltstahl <zelphirk...@gmail.com>:
So as far as I know Jupyter Notebook uses the CodeMirror editor to render code cells. In the CodeMirror documentation I found the method `doc.getValue` which is supposed to give the editor's content. According to this topic, there is no documentation for the JS API of Jupyter Notebook.

My question is: How do I get the (code) content of a/all code cell(s)? Better even would be a way to get only the code cell's contents of code cells, which are assigned a specific CSS class or other identifying feature.

You could provide metadata to the cells you want to extract, e.g.:

{
  "extract": true
}

and then, from javascript, you could do:

var cells = Jupyter.notebook.get_cells()
for (var i=0;i<cells.length;i++){
    if (cells[i].metadata['extract'] && cell.cell_type === "code"){
        console.log(cells[i].get_text())       
    }
}
 

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/996ce1d8-607f-4fa0-b389-2fdf4f19c5bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kiko

unread,
Mar 14, 2017, 7:22:24 AM3/14/17
to jup...@googlegroups.com
Sorry, a bug :-P, corrected version


var cells = Jupyter.notebook.get_cells()
for (var i=0;i<cells.length;i++){
    if (cells[i].metadata['extract'] && cells[i].cell_type === "code"){
        console.log(cells[i].get_text(
))       
    }
}

Zelphir Kaltstahl

unread,
Mar 14, 2017, 7:57:39 AM3/14/17
to Project Jupyter
Hey, thanks for the idea with the meta data! This could work well. I'll try soon.


On Tuesday, March 14, 2017 at 12:22:24 PM UTC+1, Kiko correoso garcia wrote:
Sorry, a bug :-P, corrected version

var cells = Jupyter.notebook.get_cells()
for (var i=0;i<cells.length;i++){
    if (cells[i].metadata['extract'] && cells[i].cell_type === "code"){
        console.log(cells[i].get_text(
))       
    }
}
2017-03-14 12:20 GMT+01:00 Kiko <kikoco...@gmail.com>:
2017-03-14 11:20 GMT+01:00 Zelphir Kaltstahl <zelphirk...@gmail.com>:
So as far as I know Jupyter Notebook uses the CodeMirror editor to render code cells. In the CodeMirror documentation I found the method `doc.getValue` which is supposed to give the editor's content. According to this topic, there is no documentation for the JS API of Jupyter Notebook.

My question is: How do I get the (code) content of a/all code cell(s)? Better even would be a way to get only the code cell's contents of code cells, which are assigned a specific CSS class or other identifying feature.

You could provide metadata to the cells you want to extract, e.g.:

{
  "extract": true
}

and then, from javascript, you could do:

var cells = Jupyter.notebook.get_cells()
for (var i=0;i<cells.length;i++){
    if (cells[i].metadata['extract'] && cell.cell_type === "code"){
        console.log(cells[i].get_text())       
    }
}

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.

Zelphir Kaltstahl

unread,
Mar 14, 2017, 12:19:51 PM3/14/17
to Project Jupyter
Works like a charm!


On Tuesday, March 14, 2017 at 12:22:24 PM UTC+1, Kiko correoso garcia wrote:
Sorry, a bug :-P, corrected version

var cells = Jupyter.notebook.get_cells()
for (var i=0;i<cells.length;i++){
    if (cells[i].metadata['extract'] && cells[i].cell_type === "code"){
        console.log(cells[i].get_text(
))       
    }
}
2017-03-14 12:20 GMT+01:00 Kiko <kikoco...@gmail.com>:
2017-03-14 11:20 GMT+01:00 Zelphir Kaltstahl <zelphirk...@gmail.com>:
So as far as I know Jupyter Notebook uses the CodeMirror editor to render code cells. In the CodeMirror documentation I found the method `doc.getValue` which is supposed to give the editor's content. According to this topic, there is no documentation for the JS API of Jupyter Notebook.

My question is: How do I get the (code) content of a/all code cell(s)? Better even would be a way to get only the code cell's contents of code cells, which are assigned a specific CSS class or other identifying feature.

You could provide metadata to the cells you want to extract, e.g.:

{
  "extract": true
}

and then, from javascript, you could do:

var cells = Jupyter.notebook.get_cells()
for (var i=0;i<cells.length;i++){
    if (cells[i].metadata['extract'] && cell.cell_type === "code"){
        console.log(cells[i].get_text())       
    }
}

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.

ehic...@illinois.edu

unread,
Jul 26, 2018, 3:53:02 PM7/26/18
to Project Jupyter
Hi, late to the party, but if anyone else is interested in the future, I found another way to get the code cell contents from Python. To access the code as it is going to be received for execution, you can register to the IPython event `pre_run_cell` (https://ipython.readthedocs.io/en/stable/api/generated/IPython.core.events.html#IPython.core.events.pre_run_cell). If your registered function signature takes the parameter `info`, you can access the code cell's contents with `info.raw_cell`.
Reply all
Reply to author
Forward
0 new messages