I'm using Ipython Notebook to do some data analysis and i'd like to be able to "upload" (really just copy) data files inside the Json Ipython notebook.
This way the Ipython notebook is self contained and doesn't depend on a external file position
As an example
with open("Anyfile.dat", "r") as PlainImageFile:
Would be something like
with fromNotebook("Anyfile.dat") as PlainImageFile:
No it is not possible, and it might not even make sens, as the notebook itself might not be on the machine where the kernel runs.
This is not either the goal of the Jupyter/IPython notebook format.
Moreover the kernel as no notion of wether it is runned form inside a notebook, by design. So where would fromNotebook look for the data ?
The closest to what you ask is activePapers from Konrad Hinsen, it is a fully contained environment (that also work with notebooks) ment to be fully self contained.
The second option, is to say : 1 git repository for 1 project. You can add data, images, library. Hashdist also help with that.
At least in IPython-2 it is possible to implement an Upload Widget.
See the examples in these ipynb-notebooks:
Upload-widget for IPython-2.
Upload-widget for IPython-3
Related
I was wondering if it is possible to manipulate the content of a cell of a currently running jupyter notebook from the code running inside it.
The reason I want to do this is because I have a notebook that uses some remote API and requires an APIKEY.
I have APIKEY="".... as the contents of my first cell in the notebook.
From time to time I am required to update this API KEY.
In theory what I had in mind was that if the code that runs gets an answer from API that the token has expired or is not suitable for the current IP, It could ask me for and updated APIKEY (which I copy from the back-office of the said API) and update the said cell on its own for the next runs....
I know that there might be security issues and maybe a ton of other things to consider before actually trying to do something like this - and I also assume that in theory it should be possible to manipulate the underlying notebook file - but still was wandering if jupyter exposes some sort of an API for doing this type of in-place code manipulation.
This would also make it possible to do "self evolving" code or code generation of different sorts.
Some data/value representations are working as expected when using Jupyter Notebook "native" support in VSC, but it seems that JavaScripts elements are not properly represented or previewed.
When I try to get Map (gmaps) as a figure, in VS Code, as a result, I get Figure(layout=FigureLayout(height='420px')) whereas when I execute the same code block in Jupyter Notebook/Lab server inside of Chrome browser, I get the wanted output which tells me that my external setup should be Ok. I'm using jupyter-js-widgets and jupyter-gmaps extensions.
I have a feeling that I'm missing some additional configuration(s) in VS Code regarding JavaScripts.
My VS Code About:
My Jupyter Version
Thank you
#MatejZ. Currently most widget based Jupyter extensions won't work in VSCode Notebook Editor and Interactive Window. However we are looking at add more support for this as it's a highly requested feature. If you want you can track our GitHub issue for this support (that we've already started to look into) here:
https://github.com/microsoft/vscode-python/issues/3429
Temporary solution was to add something like this:
# create html
from ipywidgets.embed import embed_minimal_html
embed_minimal_html('export.html', views=[fig], title = 'Gmap output')
which will take object "fig" and make it accessible through "export.html" page.
Thank you #Ian Huff for pointing to the issue :) Webview was mentioned in the issue, which could be coupled with the temp solution. But, I'm satisfied for now.
I'd like to take advantage of a number of features in PyCharm hence looking to port code over from my Notebooks. I've installed everything but am now faced with issues such as:
The Display function appears to fail hence dataframe outputs (used print) are not so nicely formatted. Equivalent function?
I'd like to replicate the n number of code cell in a Jupyter notebook. The Jupyter code is split over 9 cells in the one Jupyter file and shift+ Enteris an easy way to check outputs then move on. Now I've had to place all the code in the one Project/python file and have 1200 lines of code. Is there a way to section the code like it is in Jupyter? My VBA background envisions 9 routines and one additional calling routine to get the same result.
Each block of code is importing data from SQL Server and some flat files so there is some validation in between running them. I was hoping there was an alternative to manually selecting large chunks of code/executing and/or Breakpoints everytime it's run.
Any thoughts/links would be appreciated. I spent some $$ on Udemy on a PyCharm course but it does not help me with this one.
Peter
The migration part is solved in this question: convert json ipython notebook(.ipynb) to .py file, but perhaps you already knew that.
The code-splitting part is harder. One reason to why Jupyter is so widely spread is the functionality to split the output and run each cell separately. I would recommend #Andrews answer though.
If you are using classes put each class in a new file.
I have been editing a Jupyter Notebook for the past week, and tried saving it today. While attempting to save it, I got an error, so I refreshed the page and successfully saved it.
However, to my dismay almost all of my command history was lost! I still have access to the variables (the kernel never died), but I don't have access to any of the code.
Is there any way to recover the code? The kernel is still running, but I do not see any checkpoints in my notebook.
You can get the IPython history, which Jupyter Notebook uses as its kernel. In one of the cells run:
%history
This will dump a history (good, bad, and ugly) of every command you have run in the current IPython session. It is probably more than you want, but it is better than losing all your work.
Something similar happened to me where I could not save what I did, refreshed the page, and lost all my commands.
You can get your entire notebook history and redirect the output to any given file by running
%history -g -f filename
In my case, I redirected it to a (.py) file. You can find more documentation here https://ipython.readthedocs.io/en/stable/interactive/magics.html under "%history".
If your kernel hasn't restarted you can get your code from globals like _i{1,2,3,...}.
The simplest way to find your code is to run something like
print([k for k, v in globals.items() if 'some unique code snippet' in str(v)])
Say you got something like ['_i2', '_i5', '_i9'], then you can
print(_i9)
and copypaste it to a new cell.
I have successfully achieved this using the method documented at Run IPython Notebook in Iframe from another Domain . However, this required editing the user config file. I was really hoping to be able to set this up via the command-line instead (for reasons).
http://ipython.org/ipython-doc/1/config/overview.html indicates that configuration via the command line is possible. However, all the examples are for simple true/false value assignment. To set the server up to allow embedding, it is necessary to set a value inside a dictionary. I can't work out how to pass a dictionary in through the command-line.
Another acceptable option would be a configuration overrides file.
Some people will wonder -- why all this trouble!?!
First of all, this isn't for production. I'm trying to support non-developers by writing a web-based application which integrates Ipython notebooks within it using iframes. Despite being on the same machine, it appears that the different port number used is enough to mean that I can't do simple iframe embedding without setting the x-frame insecurity bit.
Being able to do this via the command line lets me set the behaviour in the launch script rather than having to bundle a special configuration file inside my app, and also write an installer.
I really hope I've make the question clear enough! Thanks for any and all suggestions and help!
Looking over the IPython source for the loaders, it seems like it will execute whatever python code you put on the right hand side. I've not tested it, but based on the link you provided, you can probably pass something like
--NotebookApp.webapp_settings=dict('headers'=dict('X-Frame-Options'='ALLOW-FROM https://example.com/'))