convert ipynb notebook to org and execute in emacs - emacs

I've seen some applauses for org-babel in place of jupyter (https://news.ycombinator.com/item?id=16842786) and trying out what it is about.
I'd like to convert *.ipynb file to *.org file and the execute each source block in org mode as we would do in jupyter notebook.
(I tried ein couple of days, but it seems unstable)
I've succeeded converting file formats as illustrated in https://www.reddit.com/r/emacs/comments/7lcgkz/import_a_jupyter_notebook_to_an_orgmode_file/
However I'm having hard time executing code blocks because variables are not shared among code blocks.
Can I use PROPERTIES: or similar method to run them in the same context?
Can I use ipython?
I failed to google how to use org mode for ipynb file (as I try to do).
Would like to know if someone shares a work process of doing it

If variables are not shared between code blocks it might be due to the lack of :session argument otherwise each code block is run in standalone mode. For instance, the second block should print i
#+begin_src python :session my_session
i = 0
#+end_src
#+begin_src python :session my_session :results output
print(i)
#+end_src
whereas this one should raised a NameError: name 'i' is not defined error
#+begin_src python :results output
print(i)
#+end_src

Related

Running Matlab in Emacs Org-mode Win10

I am looking for some direction to run Matlab in org-mode on a Win10 machine.
Matlab is installed and fully pathed within windows so it loads without issues.
Matlab is added in the org-babel languages list which loads without error.
These two lines to identify explicitely where matlab is and define header arguments which load without error.
(setq matlab-shell-command "c:/Program Files/MATLAB/R2018b/bin/matlab.exe")
(setq org-babel-default-header-args:matlab '((:results . "output") (:session . "*MATLAB*")))
When I attempt to run a something simple such as
#+begin_src matlab :results output
a=4
b=5
c=a*b
ans=c
#+end_src
I receive an error "no such file or directory, matlab".
What is the correct way to identify the location and execution of matlab to process the org-mode block?
(Note: I have extensively googled it but have not found any usable instruction from Win10 users who seem to be few in number.)

Executing a python file from IPython

I'm relatively new to NumPy/SciPy and IPython.
To execute a python script in the python interactive mode, we may use the following commands.
>>> import os
>>> os.system('executable.py')
Then the print outputs can be seen from the python prompt.
But the same idea doesn't work with IPython notebook.
In [64]:
import os
os.system('executable.py')
Out[64]:
0
In this case, I cannot see any print outputs. The notebook only tells weather execution was successful or not. Are there any ways to see the outputs when I use IPython notebook?
Use the magic function %run:
%run executable.py
This properly redirects stdout to the browser and you will see the output from the program in the notebook.
It gives you both, the typical features of running from command line plus Python tracebacks if there is exception.
Parameters after the filename are passed as command-line arguments to
the program (put in sys.argv). Then, control returns to IPython's
prompt.
This is similar to running at a system prompt python file args,
but with the advantage of giving you IPython's tracebacks, and of
loading all variables into your interactive namespace for further use
(unless -p is used, see below).
The option -t times your script. With -d it runs in the debugger pdb. More nice options to explore.

IPython | Unix commands not working in script but working in command line interpreter

Below lines when i put them in test.py and runs it, gives me error but runs fine when i run them from command line.
pyvar = 'Hello world'
!echo "A python variable: {pyvar}"
jitu#jitu-PC:~/ipython/python$ ipython test.py
File "/home/jitu/ipython/python/test.py", line 2
!echo "A python variable: {pyvar}"
^
SyntaxError: invalid syntax
Any idea why it is not working ?
.py file are python script, they are supposed to be pure python, IPython will not try to do some "magic" on it. You should rename your script to .ipy if you want to use the syntactic sugar IPython offers on top of pure python syntax.
Note that all IPython syntactic sugar can be transformed into pure python (cf %hist vs %hist -t) that will be valid python syntax, but still need to have access to an IPython instance.

How to run IPython script from the command line - syntax error with magic functions, %

I want to run IPython from the command line. However, I get a syntax error on the first line, importing pylab with the magic function %pylab is giving a syntax error on the %. The command I am using is simply ipython -i script.py.
Any ideas how to solve this?
You need to name your file script.ipy. When it ends in .ipy it can contain ipython syntax.
From ipython --help:
Usage
ipython [subcommand] [options] [files]
If invoked with no options, it executes all the files listed in sequence
and exits, use -i to enter interactive mode after running the files. Files
ending in .py will be treated as normal Python, but files ending in .ipy
can contain special IPython syntax (magic commands, shell expansions, etc.)

Simultaneously displaying and capturing standard out in IPython?

I'm interested in implementing a behavior in IPython that would be like a combination of ! and !!. I'm trying to use an IPython terminal as an adjunct to my (Windows) shell. For a long running command (e.g., a build script) I would like to be able to watch the output as it streams by as ! does. I would also like to capture the output of the command into the output history as !! does, but this defers printing anything until all output is available.
Does anyone have any suggestions as to how to implement something like this? I'm guessing that a IPython.utils.io.Tee() object would be useful here, but I don't know enough about IPython to hook this up properly.
Here is a snippet of code I just tried in iPython notebook v2.3, which seems to do what was requested:
import sys
import IPython.utils.io
outputstream = IPython.utils.io.Tee("outputfile.log", "w", channel="stdout")
outputstream.write("Hello worlds!\n")
outputstream.close()
logstream=open("outputfile.log", "r")
sys.stdout.write("Read back from log file:\n")
sys.stdout.write(logstream.read())
The log file is created in the same directory as the iPython notebook file, and the output from running this cell is displayed thus:
Hello worlds!
Read back from log file:
Hello worlds!
I haven't tried this in the iPython terminal, but see no reason it wouldn't work as well there.
(Researched and answered as part of the Oxford participation in http://aaronswartzhackathon.org)