How does the code in Jupyter Notebook get executed in Ipython - jupyter

I am new to jupyter and I modify the html content of the generated notebook when you click create new Notebook in Jupyter. I have added an html block that contains a jupyter code to display graph or markdown code. Now, does anybody know how do I use the jupyter javascripts to interpret my code and display it inside the jupyter notebook cell index 0?. If my question is not clear please let me know and would love to edit it.
currently, I see this code in jupyter main.min.js. But how do they get the code and send it to python.
'run-cell':{
help : 'run selected cells',
help_index : 'bb',
handler : function (env) {
env.notebook.execute_selected_cells();
}
},

Related

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"

How do I edit a jupyter notebook cell's tags in visual studio code?

I am editing a .ipynb file in Visual Studio Code, with the file open in VS code's Jupyter Notebook editor.
When I edit this notebook in the Jupyter Notebook App (i.e. if I were not using VS code, instead using the interface described here), I could add tags to cells by clicking View > Cell Toolbar > Tags, and then entering tags into the UI that comes up.
Is there an equivalent way to do this in VS Code?
I am aware I can reopen the file in a text editor view and edit the JSON directly. But I am looking for something a bit more user friendly than this.
Try installing this jupyter on vs code.
On my old windows 7 I used to use this. It has the same interface as when you open Jupiter in your browser but you don't need to load a bunch of stuff to open jupyter. You can open it in one single click like changing text file while writing code inside vc code.
Also you need to install one library to use it but unfortunately I can't remember the library as I don't use python or Jupiter anymore.
In the documentation is a python code cell which enables to change all the cell tags.
import nbformat as nbf
from glob import glob
# Collect a list of all notebooks in the content folder
notebooks = glob("**/*.ipynb", recursive=True)
notebooks_windows = []
for i in notebooks:
j = i.replace("\\", "/")
notebooks_windows.append(j)
notebooks_windows
# Text to look for in adding tags
text_search_dict = {
"# HIDDEN": "remove-cell", # Remove the whole cell
"# NO CODE": "remove-input", # Remove only the input
"# HIDE CODE": "hide-input" # Hide the input w/ a button to show
}
# Search through each notebook and look for the text, add a tag if necessary
for ipath in notebooks_windows:
ntbk = nbf.read(ipath, nbf.NO_CONVERT)
for cell in ntbk.cells:
cell_tags = cell.get('metadata', {}).get('tags', [])
for key, val in text_search_dict.items():
if key in cell['source']:
if val not in cell_tags:
cell_tags.append(val)
if len(cell_tags) > 0:
cell['metadata']['tags'] = cell_tags
Microsoft have finally released an extra extension to support this.
ms-toolsai.vscode-jupyter-cell-tags
See this long running github.com/microsoft/vscode-jupyter issue #1182 and comment recently closing the issue.
Be ware however, that while you can set tags, e.g. raises-exception, which is meant to make the runtime expect and ignore the exception and keep on running more cells, but the vscode-jupyter extension that runs the cells does not always honor tag features the same way that the standard Jupyter notebook runtime does, and so, disappointingly, it's still difficult to demo common mistakes or what not to do (exceptions).
Just copy everything in .py file with the unremovable tags, paste that into a plaintext file. The tags are now just editable text. Remove them, Save As or copy/paste back to the .py file.

Is it possible to use %edit in an IPython Notebook to edit in-browser?

I have been using the magic %edit in Ipython to edit strings on the fly and was wondering whether this is also possible in an IPython Notebook without having to leave the browser (Possibly in a new cell)?
It turns out that %edit does not load variable content in a new cell in jupyter notebooks for now (https://github.com/jupyter/help/issues/147).
However, you can load variable s contents in a new cell by using %load s. you can then edit the content and store it in a new variable from within the same cell.

Can I modify contents in Jupyter Notebook output cell?

In Jupyter notebook (FKA Ipython notebook), I have a cell which I ran for a long time until I got some text result in the output cell. However I found there is a small typo in the output that I would like to change without rerunning the cell again. Is there a way to modify contents in output cells?
The .ipynb files are just text files in JSON looking format. You can search for the typo and change it.
I suggest you fix the typo in the input cell in case you run it again and then save the notebook. Then open the .ipynb file and search for the error in an output cell and fix it. Save it as text and reopen in Jupyter. It will have changed.
Open the Jupyter notebook file in a notepad and change the output as you desire.
No, the output is entirely dependent on the input cell, so you will need to modify the input and run it again.

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)