From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Kitchin Subject: Re: ipython integration fails Date: Wed, 16 Oct 2019 11:46:01 -0400 Message-ID: References: <874l09os6v.fsf@th-koeln.de> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:55510) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iKlV3-0004lq-68 for emacs-orgmode@gnu.org; Wed, 16 Oct 2019 11:46:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iKlV1-00087P-Dm for emacs-orgmode@gnu.org; Wed, 16 Oct 2019 11:46:09 -0400 Received: from mail-qt1-x841.google.com ([2607:f8b0:4864:20::841]:36594) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iKlV1-00086f-9F for emacs-orgmode@gnu.org; Wed, 16 Oct 2019 11:46:07 -0400 Received: by mail-qt1-x841.google.com with SMTP id o12so36816924qtf.3 for ; Wed, 16 Oct 2019 08:46:06 -0700 (PDT) Received: from Johns-MacBook-Air.local (KITCHIN-TIMEMACHINE.CHEME.CMU.EDU. [128.2.54.215]) by smtp.gmail.com with ESMTPSA id l15sm13130225qkj.16.2019.10.16.08.46.03 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 16 Oct 2019 08:46:03 -0700 (PDT) In-reply-to: <874l09os6v.fsf@th-koeln.de> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org I guess you are having a problem with the version of jupyter, and maybe jupyter_client. Maybe upgrading thises will fix the ipython problem. See notes below for python sessions. Prof. Dr. Johanna May writes: > Hi everybody, > > I am creating lecture notes including some python snippets. Now, in > order to teach jupyter notebook use I would like to include just the > same code as in a python snippet in an ipython snippet. If you get ipython working, then you can write your lectures in org-mode, and use ox-ipynb from scimax to export a jupyter notebook from the org-file. > > However ipython blocks don't work in my set up and I could not figure > out which dependency or config line I might need to add in order to do > this properly (and scimax also creates errors, but since I have my own > config running I thought ipython might already do the job). > > The ipython snippet I used is: > #+BEGIN_SRC ipython :session :results raw drawer > %matplotlib inline > import matplotlib.pyplot as plt > import numpy as np > #+END_SRC > > The error message is: > Traceback (most recent call last): > File "/home/username/.emacs.d/elpa/ob-ipython-20180224.953/client.py", line 60, in > c = create_client(args.conn_file) > File "/home/username/.emacs.d/elpa/ob-ipython-20180224.953/client.py", line 43, in create_client > cf = find_connection_file('emacs-' + name) > File "/usr/lib/python3.6/site-packages/jupyter_client/connect.py", line 218, in find_connection_file > raise IOError("Could not find %r in %r" % (filename, path)) > OSError: Could not find 'emacs-default' in ['.', '/run/user/1000/jupyter'] > > Maybe I do not need ipython anyway. What works are python code snippets > like the following one. Minted exports both, code and results and they > are both displayed in the pdf slides/manuscript (using lualatex, > beamerarticle etc.) > > #+begin_src python :results output :session :exports both > import time > time.sleep(3) > #+end_src > > However, then, what does not work properly is several code blocks in one > session, e.g. > > (1) > #+begin_src python :results output :session :exports both > import pandas as pd > import numpy as np > import matplotlib.pyplot as plt > import datetime as dt > import seaborn as sns > sns.set(style="darkgrid") > #+end_src > > (2) > #+begin_src python :results output :session :exports both > df = pd.read_csv('./household_data_1min_singleindex.csv') > #+end_src > > (3) > #+begin_src python :results output :session :exports both > zeit=df.utc_timestamp.apply(lambda x: dt.datetime.strptime(x, "%Y-%m-%dT%H:%M:%SZ")) > #+end_src > > (4) > #+begin_src python :results file :session :var matplot_lib_filename=(org-babel-temp-file "figu" ".png"),fontsize=fs :exports both > plt.rcParams.update({'font.size':fontsize}) > rcParams.update({'figure.autolayout': True}) > plt.plot(zeit,df.DE_KN_residential2_washing_machine) > plt.savefig(matplot_lib_filename) > matplot_lib_filename > #+end_src To get this to work, I used #+begin_src python :results output :session :exports both import time time.sleep(3) #+end_src #+RESULTS: : Python 3.6.0 |Anaconda custom (x86_64)| (default, Dec 23 2016, 13:19:00) : [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin : Type "help", "copyright", "credits" or "license" for more information. : >>> python.el: native completion setup loaded However, then, what does not work properly is several code blocks in one session, e.g. (1) #+begin_src python :results output :session :exports both import pandas as pd import numpy as np import matplotlib.pyplot as plt import datetime as dt import seaborn as sns sns.set(style="darkgrid") #+end_src #+RESULTS: (2) #+begin_src python :results output :session :exports both x = np.linspace(0, 1) y = x**2 #+end_src #+RESULTS: (3) #+begin_src python :results output :session :exports both fontsize=12 #+end_src (4) #+begin_src python :results file :session :var matplot_lib_filename=(org-babel-temp-file "figu" ".png") :exports both plt.rcParams.update({'font.size':fontsize}) plt.rcParams.update({'figure.autolayout': True}) plt.plot(x, y) plt.savefig(matplot_lib_filename) print(matplot_lib_filename) #+end_src 5) #+begin_src python :results file :session :var matplot_lib_filename=(org-babel-temp-file "figu" ".png") :exports both plt.figure() plt.rcParams.update({'font.size':fontsize}) plt.rcParams.update({'figure.autolayout': True}) plt.plot(x, 2 * x) plt.savefig(matplot_lib_filename) print(matplot_lib_filename) #+end_src which for me worked like I think you expect, which is your run each block sequentially. There were some errors in your last block related to the fontsize and rcParams that are different in my block. Probably I have different defaults for the results (I always use output) so I printed the results instead of using the returned value. > > I do not like the fact that plt.rcParams.update and other lines that > jupyter does not need get exported - is there a simple way to exclude > certain lines from export? I don't think so. If you want the Jupyter plots to look like these, it seems like you need to include them. Otherwise, you have to put the lines in a separate block, with :exports none in the header. > > And I do get in serious trouble when I add a second data set (another csv) and do > further analysis steps - all graphics then look the same and bad and do > not represent the actual result of the code block. It seems that all get > the same wrong name and then at each graphics location this same png > gets displayed. This isn't something I was able to reproduce. I get different temp file names with the code above. > > So, maybe there is a good tutorial on using sessions in ob-python or > some other hint that I could use? > > Is there a way to restart the (python) kernel with some header message > in the src block? In scimax you can do this on an ipython src block with :restart in the header. You can restart the Python session by killing the *Python* buffer. I think you can get something like this in a header with: #+header: :var restart=(when (get-buffer "*Python*") (kill-buffer "*Python*") 0) > > Thank you very much, > > Cheers, > > J. May -- Professor John Kitchin Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 @johnkitchin http://kitchingroup.cheme.cmu.edu