Can I modify contents in Jupyter Notebook output cell? - jupyter

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.

Related

Using vscode with Jupyter extension, how to find/replace text within a notebook cell [duplicate]

As the title says, how to find and replace text inside a single jupyter cell when using the jupyter extension in Visual Studio Code? I am familiar with ctr+h but that will replace all the occurrences in the entire jupyter notebook file. This is a really important feature for me, as I am using it a lot in jupyter on the browser.
2023-02-09 update
For the most updated VSCode, select something you would like to replace, and then F2.
Original answer
F3 do the trick. It first shows the "Find" widget for a single cell. Then click the triangle on the left, which will be expanded to a "Replace" widget.
You can select the first occurrence and then use Ctrl+D. It will select the next occurence in the cell. Repeat that until you go back to the first ocurrence and then type the new value. It will replace all the values your circled on.
In case you have changed that keyboard shortcut or if it is different you can find the correct one:
try select a cell and use Ctrl+g as I just found out accidentally.

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.

How to show (as output cell) the contents of a .py file with syntax highlighting?

I'm aware of the %load function (formerly %loadpy) which loads the contents of a file (or URL, ...) into a new input cell (which can be executed afterwards).
I'm also aware of %less, %more and %pycat, which show the contents of a file in a pager (which means in the notebook it's shown in the split-window at the bottom of the screen).
Is there a (magic) command to load a file and show its content (with syntax highlighting) in an output cell?
I.e. something like the following but with syntax highlighting of the result:
with open('my_file.py', 'r') as f:
print(f.read())
I want the file content to be stored with the .ipynb file but I don't want it to be executed when I do Cell -> Run All.
Is there a command similar to %psource which shows the source code in an output cell instead of a pager?
Example code based on answer by #Matt:
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
import IPython
with open('my_file.py') as f:
code = f.read()
formatter = HtmlFormatter()
IPython.display.HTML('<style type="text/css">{}</style>{}'.format(
formatter.get_style_defs('.highlight'),
highlight(code, PythonLexer(), formatter)))
No there is not way to do that with current magics, but it is pretty easy using pygments and returning IPython.display.HTML(...).
10 years later, and there's now a much simpler solution:
https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html#IPython.display.Code
from IPython.display import Code
Code(filename='my_file.py', language='python')

How to conver this text file to proper cell array - Matlab

Alright this is the text file
{'samsung','n150','jp0xtr','n570','2gb','320gb','10','w7str'};
{'samsung','n150','jp0xtr','beyaz','intel','atom','n570','66','ghz','2048mb','ddr3','320gb','10','cam','bt','w7str'};
{'samsung','n150','jp0xtr','atom','n570','66ghz','2gb','320gb','10','netbook','w7s','beyaz'};
{'samsung','n150','jp0xtr','atom','n570','66ghz','2gb','320gb','10','netbook','w7s','beyaz'};
{'samsung','n150','jp0xtr','intel','atom','n570','66ghz','2gb','320gb','10','win7starter'};
{'samsung','n150jp0xtr','n570','2g','320gb','10','w7s','beyaz'};
{'samsung','n150','jp0xtr'};
{'samsung','n150','jp0xtr'};
Now i want to load it as the below cell array
I used copy paste here but i want to do that via file reading (text file). How can i do that ? Thank you.
Just edit the text file to that it starts with C = { and also ends in } and change the extension to .m. Then run it as if it was a script from matlab command-line.
Next time do not copy and paste, use the save command as in save ('monitor-list.txt', 'C', '-ASCII'). You can then load it easily with load path-to-file.