iPyWidgets FileUpload not working as I expect it - jupyter-notebook

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

Related

If you run a file.to_csv code in Jupyter as opposed to Google Colab, where does the file go to be downloaded?

This is the input I'm running: file = pd.DataFrame({'column': x_test, 'target': y_test}) my_file.to_csv('file.csv', index=False).
In G Colab, the file appears in the files folder in the left column and you can download it there. I'm trying to figure how to download it in Jupyter?
Once you are done with your code as you mention in your question.
Click on File.
Select Open.
It will open a new tab where you find three menu File, Running, Clusters.
The csv file that you made by the name of "file.csv" you can find in the File section.

Preventing a cell in a Jupyter notebook from saving to disk

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.

Saving .R script File Using Script

I am using R Studio and I want to save my script (i.e., the upper left panel). However, the only ways that I can find to do it are by either clicking the blue floppy disk icon to save or using the drop down menu File > Save > name.R
Is there any way besides using these shortcuts to save the script to a .R file or is the shortcut the only way?
Thanks.
You can use rstudioapi::documentSave() to save the currently open script file to disk.
From the source documentation, one can see that it can be used in conjunction with the id returned with getActiveDocumentContext()$id to make sure the document saved is the one running the script.
For your intended use, try:
rstudioapi::documentSave(rstudioapi::getActiveDocumentContext()$id)
For future reference, here is the reference manual of rstudioapi:
https://cran.rstudio.com/web/packages/rstudioapi/rstudioapi.pdf
I'm not yet allowed to comment, but this refers to the comment above that this does not work with .rmd files:
rstudioapi::documentSave(rstudioapi::getActiveDocumentContext()$id)
I tried and in Rstudio Version 1.2.5042 it does seem to work.
Every new tab in R(created by ctrl+shift+n) can be independently saved by using ctrl+s in the respective tab. If you intend to rename the file though, you may do it as you would rename any file in windows(goto the file location and single click on the filename). Hope my answer was of some help!

JuliaBox - Console Copy Paste - Clipboard - Not working

Machine: Windows7, browser: Firefox (lastest/recent).
I'm using Julia browser console (JuliaBox) to learn Julia. It works!
I not able to do the following though.
If I select some text in the console and right click > Copy it, the data is not getting copied to the global/system clipboard buffer.
I can open a new file within the console (vi new.txt) and there if I paste the data, it works!!
But if I want to copy the same selected/copied data outside of JuliaBox console, it doesn't work (i.e. if I try to paste it to Notepad++ or in outlook email, word doc etc).
Am I missing anything?
ALSO -- If I copy anything from notepadd++ / word doc or any data from an opened app or browser window, then I CAN'T paste it to JuliaBox console!! even if I try Control+C / Right Click Copy to copy the data.
copy-paste data from JuliaBox console:
use Ctrl+c/Ctrl+v on Windows/Linux or Cmd+c/Cmd+v on Mac.
copy-paste data into JuliaBox console:
a workaround is to use the term.js widget Paste from browser in the right-click menu.
this seems to be a bug, the issue says it will be fixed in the future.

how to upload a text file into ipython notebook

I want a whole file as a text file instead of just a cell in IPython notebook.
I write some codes in IPython notebook and now I want to test them ,so I tried to upload some text file into IPython notebook as the raw data.But the files' extension are always ".ipynb" and the format of the text files have changed so my code can't read it correctly.
How could I upload a text file into Ipython notebook? thanks in advance
Can you be more elaborate?
Are you trying to read data out of the text file? In that case regular python open and read functions can get you the data into variables. You can even do Unix "cat" the contents of the file in a variable:
var=!cat file
Or are you trying to get the text in the ipynb files as content in an input cell so you can edit them and run them? The ipynb files are usually created by the notebook engine and they are JSON formatted. So if you just copy paste the text from them into a cell, they won't be usable as is. Use some JSON parsing to interpret their content. They are not regular python code so "%run file" wont work to get their code executed by ipython.
Elaborating the use cases may help others to give solutions...
The following lines open a text file and display the content in a jupyter notebook cell.
text_file = open(full/path/to/text/file)
file_content = text_file.read()
print(file_content)
text_file.close()
I hope this helps.
It may be late, you can do this
%%pycat full/path/to/text/file

Resources