VS Code code folding of Python code in Jupyter Notebooks? - visual-studio-code

I'm looking to fold code in Jupyter Notebook cells.
Note this is different from folding the whole cell or the whole output.
This illustrates the feature well (it's a Gif):
Is there any way to achieve this in VS Code?

Related

Notebook-specific settings in VS Code

I would like to specify settings in VS Code that only apply when working with a (Jupyter) notebook.
I know language-specific settings are possible with this syntax, e.g. for Python:
"[python]": {
"editor.some.setting": "something"
}
However, there seems to be no option to select python-notebook or something similar as a language. Is there any workaround for this?
(Specifically, I would like to disable the final newline for autopep8 formatting when inside a Jupiter notebook by setting "python.formatting.autopep8Args": ["--ignore", "W292"])

Make new command in .ipynb markdown in VSCode

I am trying to create new commands for markdown in an ipython notebook file in VSCode, but am having trouble doing so
This post shows an example which (kinda) works in jupyter notebook:
$\newcommand{\vect}[1]{{\mathbf{\boldsymbol{{#1}}}}}$
This is the vector $\vect{x}$.
But pasting this exact code in VSCode, I get the error:
ParseError: KaTeX parse error: Undefined control sequence: \vect at position 1: \vect{x}.
So it seems the new command does not get created. Am grateful for any solution
Issue 125425 opened by Chandresh Pant and mentioned in the comments seems to be solved for VSCode 1.69 (June 2022)
See PR 148006 and commit acb156d:
In order to make macros defined by the author persistent between KaTeX elements, we need to pass one shared macros object into every call to the renderer.
KaTeX will insert macros into that object and since it continues to exist between calls, macros will persist.
See KaTeX docs.
Try the Markdown + Math extension by Stefan Goessner which supports macros. It works really well on my setup.
We can also define macros in the user settings, e.g.
"mdmath.macros": {
"\\vect" "{\\mathbf{\\boldsymbol{{#1}}}}"
}
or in a separate json file as follows.
"mdmath.macroFile": "/path/to/macros.json"

VS Code syntax highlighting for SQL commands in Python

The first syntax highlighting is of VS Code and the second one is of Sublime Text. I searched for extensions but I couldn't find anything which could detect SQL commands like CREATE TABLE and highlight them or suggest them as I start typing.
Sublime Text and Atom have this feature by default, but I can't get it to work in VS Code.
I am working with .py files so the syntax highlighting works only for Python commands and the whole text (inside quotes) is treated as string in VS Code.
Is there any fix to get syntax highlighting like Sublime Text / Atom in VS Code when working with SQL syntax in .py files or highlighting commands even if it's inside quotes ("")?
It seems the VS Code doesn't support this feature officially.
Hence, I make an extension called Highlight String Code which can highlight SQL expressions in Python or any other language.
You can easily use it by uppercasing the first keyword of the SQL command and adding a semicolon at the end:
I hope the extension can be helpful.

Can I create an iPython notebook using JavaScript as the language in the cells?

I love IPython to explain algorithms in python. But I want to do the same using javascript. Is it possible to write a notebook where I use javascript as the cell language?
You can use the %%javascript magic function for running javascript in IPython notebook. For example Paste the following code in a IPython cell and run it. You should see the output in your browser's javascript console.
%%javascript
console.log("Hello World!")
For global variables, you can add an attribute to the windows object for
example, in a cell run the following code:
%%javascript
window.myvar = 12;
In another cell, run the following code and check browser's javascript console. The variable's value should be printed.
%%javascript
console.log(myvar)
Use the element variable for printing in the cell output area as shown below:
%%javascript
element.append(myvar)

Syntax Highlighting in iPython Notebook Markdown Cell

Is there a way to get a Markdown cell in iPython Notebooks to highlight syntax in code blocks?
For instance, in GitHub, one can get the desired effect via the following.
```python
>>>print('hello')
```
The GitHub Flavored Markdown-style of denoting code using the triple-backtick is now supported in IPython master branch on GitHub, and so will be included in the 1.0 release.
As Jakob noted, even prior to this, you could use regular markdown for code, in which you just need to indent your code by four spaces, and this continues to be a valid way of displaying code in your IPython notebook.
using IPython 0.13.1 syntax highlighting is as easy as (in a markdown cell):
some text
def foo():
print 'bar'
return 0
some text
Just, use a blank line before and indent the code (see example notebooks shipped with Ipython). This works for Python and some other languages.
In IPython 7.2.0 notebooks you can use:
Text `code` text
in a markdown cell to print highlighted code inline.