Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding syntax highlighting to Jupyter notebook cell magic

I'm trying to figure out how to activate CodeMirror syntax highlighting for a CodeMirror-supported language (cypher) within a cell for a custom Jupyter cell magic (%%mymagic). The magic isn't associated with a special kernel - it just runs Python commands that process the string entered into the cell that I want to highlight. From what I can tell, this ostensibly can be done using something like

from notebook.services.config.manager import ConfigManager
cm = ConfigManager()
cm.update('notebook', {'CodeCell': {'highlight_modes': {'magic_cypher': {'reg': '^%%mymagic'}}}})

within the class that implements the magic. I can't seem to get this to work, however; no change in highlighting occurs when I enter stuff in a cell that starts with %%mymagic. Is the above approach accurate? Does 'magic_cypher' need to have a specific format? Does the magic need to somehow specify the MIME type CodeMirror associates with the desired highlighting language? I'm using notebook 5.0.0, jupyter_core 4.3.0, and python 2.7.13.

like image 584
lebedov Avatar asked Apr 26 '17 17:04

lebedov


People also ask

What does %% do in Jupyter Notebook?

IPython Magic – Timing These are especially handy when you have some slow code and you're trying to indentify where the issue is. %%time will give you information about a single run of the code in your cell.

How do you highlight a cell in Jupyter Notebook?

Show activity on this post. Then a button with a lightbulb icon will appear on your Jupyter notebook toolbar. Pressing that button will highlight the selected lines (or, if there is no selection, the current line) in the current code cell.

Does Jupyter Notebook have syntax highlighting?

Jupyter notebook displays do not use the syntax highlighting theme in Preferences to display code blocks.

What is %% capture in Jupyter?

IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. from __future__ import print_function import sys. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.

What is cell magic in Jupyter Notebook?

In this chapter, let us understand cell magic functions and their functionalities. This cell magic function renders contents of code cell as html script. You can embed javascript code in Jupyter notebook cell with the help of this cell magic command. Contents of code cell are written to a file using this command.

How to embed JavaScript code in Jupyter Notebook cell?

This cell magic function renders contents of code cell as html script. You can embed javascript code in Jupyter notebook cell with the help of this cell magic command. Contents of code cell are written to a file using this command.

What is the difference between Jupyter Notebook cells and code cells?

Recall that a Jupyter Notebook is a series of cells that can store text or code. Cells shape a notebook’s core. Markdown Cells allows you to write and render Markdown syntax. Here’s where you can explain and document the processes. On the other hand, code cells allow you to write and run program code like Python.

How do I display Matplotlib graphs in Jupyter Notebook?

Displaying matlpotlib graphs in jupyter notebook If you are using an older version of Jupyter notebooks you need to add this line of code so the graphs created with matplotlib can render within the notebook itself. In the newer versions, this is no longer needed.


1 Answers

The following code works for SQL when placed in ~/.jupyter/custom/custom.js with notebook 5.x:

require(['notebook/js/codecell'], function(codecell) {
  codecell.CodeCell.options_default.highlight_modes['magic_text/x-mssql'] = {'reg':[/^%%sql/]} ;
  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){
  Jupyter.notebook.get_cells().map(function(cell){
      if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;
  });
});

Credit goes to Thomas K for this info!

like image 190
lebedov Avatar answered Oct 25 '22 16:10

lebedov