I have a Jupyter Python notebook where one particular cell has very large visual output (multiple megabytes of Javascript including embedded data). Is there a way to mark the cell so that the output isn't saved to the .ipynb file?
I'm not aware of a way to do this without installing a Jupyter extension. I think this can be done on the front end by, for example, creating a widget extension with a template for the JS code, then populating the template as your output, since widget state is selectively saved.
If you want a reasonable solution with a minimal amount of work and you have access to modifying the Jupyter server environment, I'd suggest using the file save hook in jupyter_notebook_config.py. There's already an example in the documentation for stripping output. I've modified it (but not tested it!) to delete the JS output of cells tagged no-save.
def scrub_output_pre_save(model, **kwargs):
"""scrub output before saving notebooks"""
# only run on notebooks
if model['type'] != 'notebook':
return
# only run on nbformat v4
if model['content']['nbformat'] != 4:
return
for cell in model['content']['cells']:
if cell['cell_type'] != 'code':
continue
metadata = cell['metadata']
if 'tags' in metadata and 'no-save' in metadata['tags']:
cell['outputs'] = []
cell['execution_count'] = None
c.FileContentsManager.pre_save_hook = scrub_output_pre_save
If you need this to be installable/reusable the above code can be packaged and distributed as a Jupyter server extension.
Related
I'm looking for a library/tool that can generate a .py file from a notebook, with the added feature that I want to control which cells (both code and markdown cells) get exported and/or excluded from the .py file. For example adding to cells something like %exclude or %include and have control at export time how to work with these tags. I found jupytext that may do this, but got me confused with the version control part / link of .ipynb/.md.
I writing a jupyter notebook, which I would like to render using Voila to create a small web app/tool.
What the tool does is to get one Geojson file containing many polygons from the user and return a ZIP file containing many GeoJSON files (a file per polygon). For example, if the user uploads a GeoJSON file with 20 polygons (they are all in the same file), then the output should be a ZIP file containing 20 separate GeoJSON files - file per polygon.
I am able to do it locally and the ZIP file is saved as needed.
However, I would like to render it using Voila so that it can later work from anywhere, meaning the ZIP file will be created in-memory/on-the-fly/as-a-buffer (not sure which term is the accurate one) and then the user will be able to download the ZIP file, via automatic download, or by clicking on a button or with a pop-up window to download, it really does not matter here.
Here is a snippet of my code (let me know if it's not enough):
def on_button_clicked(event):
with output:
clear_output()
df = gpd.GeoDataFrame().from_features(json.loads(upload.data[0])) # if the file is geojson
display(HTML(f'<h4><left>There are {len(df)} polygons in the file</left></h4>'))
# make results.zip in temp directory
# https://gist.github.com/simonthompson99/362404d6142db3ed14908244f5750d08
tmpdir = tempfile.mkdtemp()
zip_fn = os.path.join(tmpdir, 'results.zip')
zip_obj = zipfile.ZipFile(zip_fn, 'w')
for i in range(df.shape[0]):
if len(field_names_col.value)==0:
field_name = f'field_{str(i+1)}'
else:
field_name = df[field_names_col.value][i]
output_name = f'{field_name}.{output_format.value.lower()}'
df.iloc[[i]].to_file(f'{tmpdir}\\{output_name}', driver=output_format.value)
for f in glob.glob(f"{tmpdir}/*"):
zip_obj.write(f, os.path.basename(f)) # add file to archive, second argument is the structure to be represented in zip archive, i.e. this just makes flat strucutre
zip_obj.close()
button_send.on_click(on_button_clicked)
vbox_result = widgets.VBox([button_send, output])
The important part is near the end:
for f in glob.glob(f"{tmpdir}/*"):
zip_obj.write(f, os.path.basename(f)) # add file to archive, second argument is the structure to be represented in zip archive, i.e. this just makes flat strucutre
zip_obj.close()
I iterate over the temporary separate files and create a temporary ZIP file (results.zip) stored in the zip_obj. How can I "push" this ZIP object to the user for download using Jupyter Notebook?
I tried using (just before or just after zip_obj.close()):
local_file = FileLink(os.path.basename(f), result_html_prefix="Click here to download: ")
display(local_file)
But I get an error when rendering it with Voila:
Path (results.zip) doesn't exist. It may still be in the process of
being generated, or you may have the incorrect path.
For example, to save it locally I did:
with zipfile.ZipFile('c:/tool/results.zip', 'w') as zipf:
for f in tmpdir.glob("*"):
zipf.write(f, arcname=f.name)
"I iterate over the temporary separate files and create a temporary ZIP file (results.zip) stored in the zip_obj. How can I "push" this ZIP object to the user for download using Jupyter Notebook?"
There's an example of making a download link that will show up in the Voila rendering here. (Learned of it from here, although the focus there was on file upload. The example covers downloading the results, too.) A simpler take on this is found at the bottom of this answer here, converted to your case:
%%html
Download the Resulting Files as an Archive
These two options are illustrated for a different type of file in the bottom section (everything below the line break presently there) of the answer here.
Im currently trying to load a file in my jupyter notebook, to read it's content.
My Code:
file = widgets.FileUpload(
description= 'Title',
accept='.csv',
multiple=False,
)
box = widgets.HBox([file])
display(box)
# New Cell
print(str(file))
I run the first cell, after that i use the FileUploader to Upload a .csv File before running the second cell.
I expect the output to be a dict containing the files Context like "content" etc.
A Screenshot of a simpler version:
Does anyone know that i'm doing wrong?
I also tried every other idea that came to my mind.
Unfortunately, at the moment file upload widget only retrieves file value if you are using classic Jupyter lab through your browser, and IDEs such as Pycharm and VScode do not support the feature.
Open the notebook on classic Jupyter lab and try to upload the file there, and you will see your code works perfectly.
Try accessing file.value instead.
More info here: https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20List.html#file-upload
I mistakenly printed to much to the output during a single cell's execution and now the browser tab completely freezes every time that notebook is opened. I tried restarting ipython and it didn't help (I am guessing that each time it is loaded, also all the chunk of text is loaded with it).
Is there a way to load a notebook with outputs suspended or clear?
One hack if you're desperate: open the .ipynb file, which is a text file. Scroll down to the lengthy cell output and delete it. Of course, you need to be careful that the result is still a valid .ipynb file.
nbstripout is a simple tool that removes all output from a notebook (without needing to open the notebook in your browser).
your code will be saved in the form of JSON. open it with json viewer and carefully delete the unwanted output cell and save it back.
Is there a way to display past commands in R/R Studio?? I know in R Studio there is a shortcut (CTRL+UP Arrow) that allows you to see past lines you have ran. But this shortcut only allows you to access only a single line, not a block of previously run code. Is there a package, or some way in R to display and select blocks of past code in R/R Studio?
Here you can find a description of the panes in R Studio, including the "history" pane.
There you can select several lines and paste them into the code.
Also, you can use the command savehistory() to save your history in a file you can the modify. If you want to choose the name of the file to be saved, use
savehistory(file = filename)
The same option is available in the basic R GUI (MS Windows), with "Save history" in the "File" menu (as a .RHistory file). Then, you can open it with any text editor and modify your history to make a script.
To see a specific number of lines, you can use history(25) (for 25 previous lines).