Ipython Notebook: programmatically read and execute cells - macros

In the ipython notebook, I would like to programmatically read and execute code cells from within a code cell itself.
Something like
if condition:
# run input cell no. 3
I found a solution here, the function execute_notebook reads an ipynb file cell by cell and executes code cells using get_ipython().run_cell().
Is there a way to do the same, i.e. without reading the cells from the an external ipynb file first? Is there a way to write macros, to reference and access code cells from within an ipython notebook?

I'm not sure if I understand your question correctly. Would you like to be able to manipulate the exact same notebook that you are currently working in? While this may be possible, it honestly sounds like a recipe for disaster because it will most likely leave your currently running notebook in an undefined state (or at the very least it would make it hard to follow what's going on).
A cleaner solution may be to programmatically generate a separate notebook in which you include precisely the code cells you need for your use case, and then execute it. (Alternatively, you can have a "template" .ipynb file with dummy code cells which you then programmatically replace with the actual code that you want to run).
I needed to do a similar thing recently and have written up a detailed example which shows how to do this. It programmatically generates a new notebook, adds a few markdown and code cells (which in this case produce a sine wave plot where the frequency is injected on-the-fly), then executes this notebook and saves the result for further postprocessing.
I hope this helps. Let me know if I completely misunderstood what you are trying to do.

hi the exact answer to your question is :
import io
from IPython.nbformat import current
with io.open("Name_of_your_notebook.ipynb") as f:
nb = current.read(f, 'json')
ip = get_ipython()
for cell in nb.worksheets[0].cells:
if cell.cell_type != 'code':
continue
if cell.prompt_number==4186:
ip.run_cell(cell.input)
but keep in mid please, the cell will be run under this code, not like as function in py does

I think the best way in this case would be to write a procedure and run it as much as you want. But as a brain workout, I would suggest two options.
To execute the code from the cell when some condition is met (suppose we want to reuse the third one as in your example), you can use exec
if condition:
exec(In[3])
More reliable solution may be with marked cells and a magic command rerun:
if condition:
%rerun -g 'unique mark'
But again, I'm pretty sure the best way is to write a procedure and reuse it.

Related

How do I get more than one worksheet in an ipython notebook?

IPython's notebook source code, and various code samples, suggest that a notebook can consist of several "worksheets". What does a worksheet look like, and how do I create them or switch between them? I see nothing in the browser interface that would allow me to create a second worksheet.
According to IPEP 17 (link), multiple worksheets on a single notebook will be deprecated, and a UI to handle them never existed either. Before, as in your link, the format of the notebook was
notebook["worksheets"]["cells"]
but it will soon be simply
notebook["cells"]
IPEP 17 is however an active IPEP, so changes may still occur at this stage (it is not accepted or implemented yet, see this list of IPEPs and their status)
EDIT:
as a personal comment, having multiple worksheets like in Microsoft Excel may look like a nice idea, but you would often end up having many race conditions between sheets, with one cell of a worksheet depending on the output of a cell in another worksheet, often not in a self-evident way. And a scenario where many worksheets do not depend on each other is as good as a directory with as many notebooks.

How to pass between guis in matlab

I am doing a group project and we are supposed to be using GUIS to create a presentation. In the presentation there is data that is loaded in using an edit text box and our code then goes through calculations and if statements and is supposed to pass certain values to a different GUI. Our instructors do not know how to do it because we have asked them on several occasions and they cannot figure it out. We have tried varargin method and the getappdata method and neither one is working. Does anyone have suggestions? PLEEAASE!!
I would write some "Controller" program that will know open the figures for you and know how to forward the data. Basically it would be a simplified mvc-pattern. It could also assure the figures are opened in correct order.

Can IPython cell magics get access to a unique cell identifier?

I'm trying to implement magics to support a programming language where entire modules must be compiled at once. My goal is that all cells in a notebook with a particular cell magic will be coalesced into a single module M.
To make this work, when a cell C changes I need to remove the old contents of C from M and replace them with the new contents of C. However when the cell magic is called for C, there is no way to tell that it is C and not any other old or new cell. What I need is an identifier that is constant between calls to the cell magic.
Is this kind of cell identifier available anywhere in the IPython API?
No: the kernel (where execution happens) knows nothing about the notebook interface. From the kernels point of view, it receives some code to execute, and sends back some results. So running the same cell twice is exactly the same as running two cells with the same content.
I don't know what language you're working on, but other magics that require compilation treat one cell as one module. For instance, have a look at the %%cython magic.

Matlab function signature changes

Let us say that I have a Matlab function and I change its signature (i.e. add parameter). As Matlab does not 'compile' is there an easy way to determine which other functions do not use the right signature (i.e. submits the additional parameter). I do not want to determine this at runtime (i.e. get an error message) or have to do text searches. Hope this makes sense. Any feedback would be very much appreciated. Many thanks.
If I understand you correctly, you want to change a function's signature and find all functions/scripts/classes that call it in the "old" way, and change it to the "new" way.
You also indicated you don't want to do it at runtime, or do text searches, but there is no way to detect "incorrect" calls at "parse-time", so I'm afraid these demands leave no option at all to detect old function calls...
What I would do in that case is temporarily add a few lines to the new function:
function myFunc(param1, param2, newParam) % <-- the NEW signature
if nargin == 2
clc, error('old call detected.'); end
and then run the main script/function/whatever in which this function resides. You'll get one error for each time something calls the function incorrectly, along with the error stack in the Matlab command window.
It is then a matter of clicking on the link in the bottom of the error stack, correct the function call, and repeat from the top until no more errors occur.
Don't forget to remove these lines when you're done, or better, replace the word error with warning just to capture anything that was missed.
Better yet: if you're on linux, a text search would be a matter of
$ grep -l 'myFunc(.*,.*); *.m'
which will list all the files having the "incorrect" call. That's not too difficult I'd say...You can probably do a similar thing with the standard windows search, but I can't test that right now.
This is more or less what the dependency report was invented for. Using that tool, you can find what functions/scripts call your altered function. Then it is just a question of manually inspecting every occurrence.
However, I'd advise to make your changes to the function signature such that backwards compatibility is maintained. You can do so by specifying default values for new parameters and/or issuing a warning in those scenarios. That way, your code will run, and you will get run-time hints of deprecated code (which is more or less a necessary evil in interpreted/dynamic languages).
For many dynamic languages (and MATLAB specifically) it is generally impossible to fully inspect the code without the interpreter executing the code. Just imagine the following piece of code:
x = magic(10);
In general, you'd say that the magic function is called. However, magic could map to a totally different function. This could be done in ways that are invisible to a static analysis tool (such as the dependency report): e.g. eval('magic = 1:100;');.
The only way is to go through your whole code base, either inspecting every occurrence manually (which can be found easily with a text search) or by running a test that fully covers your code base.
edit:
There is however a way to access intermediate outputs of the MATLAB parser. This can be accessed using the undocumented and unsupported mtree function (which can be called like this: t = mtree(file, '-file'); for every file in your code base). Using the resulting structure you might be able to find calls with a certain amount of parameters.

Remove progress bar from published document in MATLAB

I am using the publishing functionality of MATLAB to generate a quick report of some analysis I'm running. Since the analysis is quite time-consuming, I've added a progress bar to keep track of how long is remaining. The problem is that I'd prefer this progress bar not to appear in my report.
Is there a way to keep MATLAB from introducing some content in a published document. Or, alternatively, is there a way I can know I'm currently in publish mode, so I can skip the progress bar in those cases?
Edit:
There's a couple of solutions already, but I'd prefer something automatic that doesn't require an extra step in the workspace before publication. Any other tricks?
AFAIK there is no way of excluding parts from published document.
Perhaps what you can do is to output a unique pattern (BEGIN/END) around the progress bar code, which you will then parse the html file and remove those sections using some script.
I'm assuming you're using the WAITBAR function to generate a progress bar, and you have only one of these waitbars in your function.
Before you publish the file pre-create the waitbar:
h = waitbar(0);
Then make the waitbar invisible to the PUBLISH function:
set(h,'HandleVisibility','off')
Where you use the waitbar in your code, you have to specify that you want to reuse the hidden waitbar by referring to it again, with the handle, h:
waitbar(newPercentage,h);
see the function reference page for waitbar for more help.
Another slightly more generic option (inspired by Mike Katz' response), which works for any kind of content you don't want (or explicitly want) to include in your report.
in your module/function
try
inPublishMode = evalin('base', 'inPublish');
catch
inPublishMode = false;
end
You can now set the inPublish variable from the workspace before running your test, and wrap your optional code in conditional statements.
if inPublishMode
% do something
end
Still not perfectly satisfactory, but it's another tool in the bag.