IPython change input cell syntax highlighting logic for entire session - python

You can use extensions or display helpers in IPython to make whatever syntax highlighting you'd like on output cells.
For some special cell magics, like %%javascript you can also see the input cell itself is rendered with that language's natural syntax highlighting.
How can you cause every input cell to be displayed with some chosen, non-Python syntax highlighting (regardless of any magics used on a cell, regardless of whether the cell embodies Python code, some other language).
In my case I am working with a custom-made cell magic for a proprietary language. The %%javascript syntax highlighting works well for this language, but if I have my own %%proprietarylang magic function, I obviously can't use %%javascript to help me with how the cell is displayed.
Is there a setting I can enable when I launch the notebook, or some property of the ipython object itself that can be programmatically set inside of my magic function, which will cause the same display logic to happen as if it was %%javascript.
I know that general-purpose on-the-fly syntax highlighting is not supported by the notebook currently. I'm asking specifically about how to make use of pre-existing syntax highlighting effects, such as that of %%javascript.
I've seen some documentation referring to IPython.config.cell_magic_highlight but this does not seem to exist anymore. Is there a standard replacement for it?

To replace IPython.config.cell_magic_highlight, you can use something like
import IPython
js = "IPython.CodeCell.config_defaults.highlight_modes['magic_fortran'] = {'reg':[/^%%fortran/]};"
IPython.core.display.display_javascript(js, raw=True)
so cells which begin with %%fortran will be syntax-highlighted like FORTRAN. (However, they will still be evaluated as python if you do only this.)

For recent IPython version, the selected answer no longer works. The 'config_default' property was renamed options_default (Ipython 6.0.0).
The following works:
import IPython
js = "IPython.CodeCell.options_default.highlight_modes['magic_fortran'] = {'reg':[/^%%fortran/]};"
IPython.core.display.display_javascript(js, raw=True)

Related

Custom highlighting for string literals in Jupyter

Is there prior art to either
(Simple?) Add a new cell type to Jupyter notebook and apply custom syntax highlighting to all string literals in cells of that type while otherwise treating these cells exactly like regular Python cells. I'd be fine to do the parsing myself, but I'm not sure how one would split the highlighting work between Jupyter and my custom highlighter. I'd be happy to dig around the Jupyter source code to make this work, i.e., I'd accept this not having the form of a regular Jupyter extension in the end.
(Hard?) Apply custom syntax highlighting to all strings with a s prefix (e.g. s'myString') and before executing the cell remove the s while keeping any additional valid python string prefixes like f or u or r)
?
If not, could someone sketch the steps required to make this work? I don't know much JS so I'd prefer Python solutions, but I'm happy to learn the required JS if needed.

How can I go to a function's definition in Jupyter notebook?

How can i go to a function definition in Jupyter?
I want something like Visual studio's f12, or eclipse's and PyCharm's ctrl+click.
I find it hard to believe this does not exist, yet couldn't find it
I'm not aware of such feature that will work for all kernels.
If you are using a Python kernel and have ipython installed you can use inspection functions:
%pdoc <object>: Print (or run through a pager if too long) the docstring for an object. If the given object is a class, it will print
both the class and theconstructor docstrings.
%pdef <object>: Print the call signature for any callable object. If the object is a class, print the constructor information.
%psource <object>: Print (or run through a pager if too long) the source code for an object.
%pfile <object>: Show the entire source file where an object was defined via a pager, opening it at the line where the object
definition begins.
%who/%whos: These functions give information about identifiers you have defined interactively (not things you loaded or defined in
your configuration files). %who just prints a list of identifiers and
%whos prints a table with some basic details about each identifier.
Typing ??word or word?? gives access to the full information,
including the source code where possible. Long strings are not
snipped.
Usage Example
In [4]: pd.DataFrame?
In [5]: pd.DataFrame??
In [6]: %pdef pd.Dataframe
Object `pd.Dataframe` not found.
In [7]: %pdef pd.DataFrame
Class constructor information:
pd.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
In [8]: %pdoc pd.DataFrame
In [9]: %pfile pd.DataFrame
Resources
Dynamic object information
If you can upgrade to JupyterLab, you could install jupyterlab-go-to-definition extension with:
jupyter labextension install #krassowski/jupyterlab_go_to_definition
It supports R and Python and allows to jump to definitions with alt-click (where alt can be changed to another key modifier in the settings). For more usage details see the linked GitHub repository (above).
On the technical side, there are two ways to get the location of definitions in a notebook: static analysis and inspection in the kernel. If you cannot upgrade to JupyterLab yet, you could try to re-implement either of those for jupyter-notebook.
The kernel inspections may seem easier to implement because they do not depend on the code editor used, but have to be written for each language separately. Here is an example for Python
static analysis can be done via external tool but those will be problematic due to the nature of the notebook (being composed of multiple cells, unlike normal source code files); the approach used in the extension above is to analyze the tokens of CodeMirror editor, iterating over cells, starting from the cell with the symbol usage (where the user alt-clicked on a variable). Here is an example of implementation for such iteration.
Disclaimer: I am the author of this extension.
Edit: For an implementation which allows the same for any language (provided that you install a language server for this language), see: jupyterlab-lsp (with many other features too!). The difference is that it only uses static analysis for now (the kernel-based position retrieval is not implemented just yet).
I used below to return call signature of function, taken from above
np.matmul??
If you have the function typed out, you can use keyboard combination 'Shift+Tab' + 'Shift+Tab' to get tool tip in notebook when in edit mode, as shown below.

Highlight initial variable assignment in python with VSCode

I would like to use tokenColor in Visual Studio Code to highlight the initial binding of a variable in python. That is to say, the first time a variable name appears in its scope, I would like it to have a different color. This would usually be where the keyword "let" or "var" would be used in JavaScript. How would I go about adding this to VSCode?
This is not possible using a simple language grammar as it requires understanding the structure of the program itself (i.e. understanding what are initial bindings vs re-assignments). This type of highlighting is called semantic highlighting.
As of VS Code 1.29, you can implement a custom version of semantic coloring using decorators. A proper semantic highlighting api is tracked by this issue

How to change default markdown's inline highlight style?

I'm using pelican in Python to generate the static pages. It is fine to use markdown with Pygment to highlight a code block, however when it comes to the inline highlight, there is a significant difference.
Please check my page. The page is in a dark background, but the inline highlight with the back ticks `` are white background.
The markdown code is
To clearify what exactly the command returns,
one can use `pack('>i', -2500).encode('hex')`{.python}.
Or I have a more complex solution like, `''.join(r'\x%02X'
% ord(ch) for ch in src)`{.python}.
So the question here is how to define the inline highlight style?
I tried to altered the pygments default.css file in the pelican theme, but it didn't work.
I found some related links:
Inline code syntax highlighting in GitHub markdown?
Inline code highlighting in reStructuredText
Pelican uses Python-Markdown with the CodeHilite Extension to pass code blocks to Pygments. Note that code blocks do not include inline code snippets. In fact I am not aware of any Markdown parser which supports highlighting inline code. Generally, inline code snippets are too short to effectively highlight and almost always too short to take advantage of Pygments' language guessing algorithm.
I see you have attempted to inform Markdown of the language using the Attribute List Extension. However, all that does is add a class to the HTML <code> tag that wraps the code snippet. As the CodeHilite Extension only works on blocks, it never even looks at it, and certainly never passes it to Pygments.
There are a few different options you have available (each increasing in the amount of work required):
You can write your own CSS which changes the background and text color so that they match the blocks. You won't get syntax highlighting, but the inline code snippets will at least match your theme. No need to define the language here.
You could use a JavaScript code highlighting library to highlight all the code snippets based on the language you define with the Attribute List Extension. However, this may require you to redefine the CSS for the JS library so it matches the output of Pygments.
You could write your own Python-Markdown extension (or perhaps fork CodeHilite) which also highlights inline code snippets in addition to blocks.
Personally, I would recommend (1) above. In fact, looking at your page, is appears that the code blocks have the following defined:
.highlight pre, .highlighttable pre {
background: #272822;
color: #f8f8f2;
}
That is, a <pre> element with a parent assigned the "highlight" class (or the "highlighttable" class) will be displayed with those colors. As you want the same colors assigned to inline code, add the following rule:
code {
background: #272822;
color: #f8f8f2;
}
Just be sure that you define that after the styles defined by the Pelican theme (which appears to be in http://vfxware.com/theme/css/bootstrap.slate.min.css).
Sure, you could alter the existing CSS, but then if you later decide to update to a newer version of the theme or change themes entirely, you might have to redefine your own rules. Therefore, I like to define my own set of overrides in a separate file which I always make sure is the last one loaded.
I've been bitten by this all too often. My problem was that I was using an older template's old pygment.css file while upgrading the Pygment modules (CSS didn't catch up with the Pygment plugin.
In my case, the older Pygment CSS was using an HTML tag class highlight when it should be using the newer codehilite class name that newer Pygment code is now using. (Thanks to Firefox->Windows->Web Developer->Toggle menu option for pointing out this "breakage".
Replacing the pygments.css CSS-style file does the trick in restoring this much needed code syntax highlighting capability.
To regenerate the pygments.css file, execute:
pygmentize -S default -f html -a .codehilite > pygments.css
or for a darker theme, use Pygmentize monokai style, as listed in pygmentize -L:
pygmentize -S monokai -f html -a .codehilite > pygments.css
Again, problem was that Pygments changed its HTTP tag class to a newer codehilite from an older highlight.

Is there a way to search for text in the ipython qtconsole?

I'm using ipython with qtconsole.
A feature I'm missing from the regular console is to search the console output.
I'd expect to see it under the Edit menu.
Is there a way to do it?
I don't know of that feature, but you can take advantage of the Out variable (also available as _oh) that is automatically exposed in the ipython console. Basically it's a dictionary in which the keys are the line numbers for which some kind of result was returned and the values are the results themselves. Hence, if you look for something in Out.values() using any python code you prefer should be useful to find what you're looking for.

Categories