Last active
February 12, 2023 07:37
-
-
Save Superbil/5899556 to your computer and use it in GitHub Desktop.
[PyHUG] July: Use Python in Org-mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #+TITLE: [PyHUG] July: Use Python in Org-mode | |
| #+DATE: <2013-07-01 Mon> | |
| #+AUTHOR: Superbil | |
| #+EMAIL: superbil@gmail.com | |
| #+EXCLUDE_TAGS: noexport | |
| #+KEYWORDS: emacs org-mode python pyhug | |
| #+OPTIONS: html-postamble:auto html-preamble:t tex:t ^:nil | |
| #+CREATOR: <a href="http://www.gnu.org/software/emacs/">Emacs</a> 24.3.1 (<a href="http://orgmode.org">Org</a> mode 8.0.3) | |
| * About :noexport: | |
| [[https://gist.github.com/Superbil/5899556][talk-org-mode.org]] | |
| * What is Org-mode ? | |
| #+BEGIN_QUOTE | |
| Org mode is for keeping notes, maintaining TODO lists, planning projects, | |
| and authoring documents with a fast and effective plain-text system. | |
| #+END_QUOTE | |
| - an outline extension with visibility cycling and structure editing | |
| - an ASCII system and table editor for taking structured notes | |
| - a TODO list editor | |
| - a full agenda and planner with deadlines and work scheduling | |
| - an environment in which to implement David Allen's GTD system | |
| - a simple hypertext system, with HTML and LaTeX export | |
| - a publishing tool to create a set of interlinked web pages | |
| - an environment for literate programming | |
| #+name: banner | |
| #+begin_html | |
| <div id="subtitle" style="float: center; text-align: center;"> | |
| <p> | |
| Org Mode support for <a href="http://python.org/">Python</a> | |
| </p> | |
| <p> | |
| <a href="http://python.org/"> | |
| <img src="http://www.python.org/images/python-logo.gif"/> | |
| </a> | |
| </p> | |
| </div> | |
| #+end_html | |
| * What is Emacs ? | |
| ** Emacs is a editor | |
| GNU Emacs is an extensible, customizable text editor—and more. | |
| At its core is an interpreter for Emacs Lisp, a dialect of the | |
| Lisp programming language with extensions to support text editing. | |
| ** When I start use Emacs ? | |
| I ask somebody "What editor do you use to write python?", when I first at PyHUG. | |
| Answer is Emacs, Vim, Emacs. So I start to use Emacs ! | |
| ** Why I still use it ? | |
| - You can hack it ! | |
| - *You don't remove your hand from the keyboard.* | |
| *** Emacs is better than X | |
| No good or bad, only it is appropriate | |
| **** *沒有好或不好,只有合不合用* :noexport: | |
| *** Vimer will say | |
| [[file:images/emacs_on_server.png]] \\ | |
| from [[http://sachachua.com/blog/2013/05/how-to-learn-emacs-a-hand-drawn-one-pager-for-beginners/][How to Learn Emacs: A Hand-drawn One-pager for Beginners]] | |
| *** It's good to do everything in Emacs. | |
| I use Emacs to ... | |
| - write code (python-mode) | |
| - use SCM (magit) | |
| - organizational life (org-mode) | |
| - write document (org-mode) | |
| - use shell (eshell) | |
| - use term (zsh) | |
| * Introduction Org-mode | |
| ** Installation | |
| *** Emacs < 24 | |
| [[http://orgmode.org/elpa.html][install from elpa]] | |
| *** Emacs >= 24 | |
| org-mode is already bundle in it. | |
| ** Org-mode structure | |
| Only highlight some structure I always use | |
| *** Headlines | |
| Headlines define the structure of an outline tree. The headlines in Org start with one or more stars, on the left margin4 5. For example: | |
| #+BEGIN_EXAMPLE | |
| ,* Top level headline | |
| ,** Second level | |
| ,*** 3rd level | |
| some text | |
| ,*** 3rd level | |
| more text | |
| ,* Another top level headline | |
| #+END_EXAMPLE | |
| *** Plain lists | |
| Unordered list items start with ‘-’, ‘+’, or ‘*’ as bullets. | |
| #+BEGIN_EXAMPLE | |
| ** Lord of the Rings | |
| My favorite scenes are (in this order) | |
| 1. The attack of the Rohirrim | |
| 2. Eowyn's fight with the witch king | |
| + this was already my favorite scene in the book | |
| + I really like Miranda Otto. | |
| 3. Peter Jackson being shot by Legolas | |
| - on DVD only | |
| He makes a really funny face when it happens. | |
| But in the end, no individual scenes matter but the film as a whole. | |
| Important actors in this film are: | |
| - Elijah Wood :: He plays Frodo | |
| - Sean Austin :: He plays Sam, Frodo's friend. I still remember | |
| him very well from his role as Mikey Walsh in The Goonies. | |
| #+END_EXAMPLE | |
| *** Table | |
| draw plain table like this | |
| #+BEGIN_EXAMPLE | |
| | Name | Phone | Age | | |
| |-------+-------+-----| | |
| | Peter | 1234 | 17 | | |
| | Bill | 4321 | 25 | | |
| #+END_EXAMPLE | |
| **** Example :noexport: | |
| | Name | Phone | Age | | |
| |-------+-------+-----| | |
| | Peter | 1234 | 17 | | |
| | Bill | 4321 | 25 | | |
| *** Hyperlinks | |
| plain URL-like links and activate them as clickable links. The general link format, however, looks like this: | |
| #+BEGIN_EXAMPLE | |
| [[link][description]] or [[link]] | |
| #+END_EXAMPLE | |
| [[http://www.meetup.com/pythonhug/][Python Hsinchu User Group (Hsinchu) - Meetup]] | |
| *** =TODO= items | |
| just add TODO keyword at head, looks like this: | |
| #+BEGIN_EXAMPLE | |
| *** TODO Write documents about org-mode | |
| #+END_EXAMPLE | |
| **** Example :noexport: | |
| ***** TODO Write documents about org-mode | |
| *** Breaking tasks down into subtasks | |
| #+BEGIN_EXAMPLE | |
| * Organize Party [33%] | |
| ** TODO Call people [1/2] | |
| *** TODO Peter | |
| *** DONE Sarah | |
| ** TODO Buy food | |
| ** DONE Talk to neighbor | |
| #+END_EXAMPLE | |
| **** Example :noexport: | |
| ***** Organize Party [33%] | |
| ****** TODO Call people [1/2] | |
| ******** TODO Peter | |
| ******** DONE Sarah | |
| ****** TODO Buy food | |
| ****** DONE Talk to neighbor | |
| *** Checkboxes | |
| Every item in a plain list can be made into a checkbox by starting it with the string ‘[ ]’. | |
| Use C-c C-c or mouse to change state. | |
| #+BEGIN_EXAMPLE | |
| * TODO Organize party [2/4] | |
| - [-] call people [1/3] | |
| - [ ] Peter | |
| - [X] Sarah | |
| - [ ] Sam | |
| - [X] order food | |
| - [ ] think about what music to play | |
| - [X] talk to the neighbors | |
| #+END_EXAMPLE | |
| **** Example :noexport: | |
| ***** TODO Organize party [2/4] | |
| - [-] call people [1/3] | |
| - [ ] Peter | |
| - [X] Sarah | |
| - [ ] Sam | |
| - [X] order food | |
| - [ ] think about what music to play | |
| - [X] talk to the neighbors | |
| *** Tags | |
| Tags make use of the hierarchical structure of outline trees. If a heading has a certain tag, all subheadings will inherit the tag as well. For example, in the list | |
| #+BEGIN_EXAMPLE | |
| * Meeting with the French group :work: | |
| ** Summary by Frank :boss:notes: | |
| *** TODO Prepare slides for him :action: | |
| #+END_EXAMPLE | |
| **** Example :noexport: | |
| ***** Meeting with the French group :work: | |
| ****** Summary by Frank :boss:notes: | |
| ******* TODO Prepare slides for him :action: | |
| *** Code block | |
| #+BEGIN_EXAMPLE | |
| #+BEGIN_SRC emacs-lisp | |
| (defun org-xor (a b) | |
| "Exclusive or." | |
| (if a (not b) b)) | |
| #+END_SRC | |
| #+END_EXAMPLE | |
| * Use python in org-mode | |
| ** Setup | |
| let org-babel can use python | |
| #+BEGIN_SRC lisp | |
| (org-babel-do-load-languages | |
| 'org-babel-load-languages | |
| '((python . t))) | |
| #+END_SRC | |
| or customizable this org-babel-load-languages | |
| ** Header Arguments | |
| *** Language-Specific Header Arguments | |
| - =:results {output, value}=: Value mode is the default (as with | |
| other languages). In value mode you can use the following subtypes: | |
| - =raw=: value is inserted directly | |
| - =pp=: value is pretty-printed by python using =pprint.pformat(%s)=, then inserted | |
| - =file=: value is interpreted as a filename to be interpolated | |
| when exporting; commonly used for graphics output. | |
| - =:preamble=: Preamble code, inserted before the body (not commonly | |
| used). Default is none. | |
| - =:return=: Value to return (only when result-type is value, and not | |
| in session mode; not commonly used). Default is None; in | |
| non-session mode use return() to return a value. | |
| *** Common Header Arguments | |
| - :session [name]: | |
| default is no session. | |
| - :var data=data-table: | |
| Variables can be passed into python from org-mode tables as scalars or lists. | |
| See the org-mode manual for more details. | |
| - :exports {code, results, both, none}: | |
| Standard babel option for what to export. | |
| ** Sessions | |
| Session can share | |
| *** origin source :noexport: | |
| Session mode is fully supported in python, including named sessions. | |
| In session mode, each block is run in the same long-running python | |
| interactive interpreter session, as if you had typed that block into | |
| python. You can have multiple sessions, all independent. | |
| Sessions can be used to define functions, set up variables, and share | |
| code between source blocks. | |
| Session mode in python is slightly different from non-session mode, | |
| because in session mode you are talking to a single "interactive" | |
| python session. In python's interactive mode, blank lines are | |
| special: they indicate the end of an indented block. So you have to | |
| write your org-mode python a little differently when using session | |
| mode. | |
| Also, in non-session mode, the python code block will be wrapped in a | |
| function, so to return a value (in =:results value= mode) you have to | |
| use a =return= statement. In session mode, the python code is | |
| evaluated directly by the interpreter, not in a function context, and | |
| the last statement's value will be automatically returned, so you must | |
| _not_ use a =return= statement. | |
| *** Session mode: | |
| blank lines not OK in indented blocks, and don't use return()\\ | |
| Source block is passed directly to interactive python;\\ | |
| value is value of _ at end. | |
| #+begin_src python :session | |
| def foo(x): | |
| if x>0: | |
| return x+1 | |
| else: | |
| return x-1 | |
| foo(1) | |
| # _ = foo(1) is ok too | |
| #+end_src | |
| #+RESULTS: | |
| : 2 | |
| *** Non-session mode: | |
| blank lines OK in indented blocks, and use return()\\ | |
| Entire source block will get indented and used as the body of main() | |
| #+begin_src python | |
| def foo(x): | |
| if x>0: | |
| return x+1 | |
| else: | |
| return x-1 | |
| return foo(5) | |
| #+end_src | |
| #+RESULTS: | |
| : 6 | |
| ** Result Types | |
| - value: | |
| Value results are the value of the last expression evaluated in the code block. | |
| This is found in session mode using using the "_" special python interpreter variable. | |
| - output: | |
| Output results come from whatever the python code prints on stdout. | |
| * Example of use python in org-mode | |
| ** Hello World! | |
| #+begin_src python :results output | |
| print "Hello, world!" | |
| #+end_src python | |
| #+RESULTS: | |
| : Hello, world! | |
| ** Inline calling: | |
| #+BEGIN_EXAMPLE :exports code | |
| Two plus two equals src_python{return(2+2)} | |
| #+END_EXAMPLE | |
| when exported, e.g. to HTML or LaTeX/PDF, becomes: | |
| #+BEGIN_EXAMPLE | |
| Two plus two equals 4 | |
| #+END_EXAMPLE | |
| ** Extracting data from an org-mode table | |
| #+tblname: data_table | |
| | a | 1 | | |
| | b | 2 | | |
| | c | 3 | | |
| #+begin_src python :var val=1 :var data=data_table | |
| # Return row specified by val. | |
| # In non-session mode, use return to return results. | |
| return(data[val]) | |
| #+end_src | |
| #+RESULTS: | |
| | b | 2 | | |
| ** Plotting | |
| #+BEGIN_SRC python :results file | |
| import matplotlib, numpy | |
| matplotlib.use('Agg') | |
| import matplotlib.pyplot as plt | |
| fig=plt.figure(figsize=(4,2)) | |
| x=numpy.linspace(-15,15) | |
| plt.plot(numpy.sin(x)/x) | |
| fig.tight_layout() | |
| plt.savefig('images/python-matplot-fig.png') | |
| return 'images/python-matplot-fig.png' # return filename to or | |
| #+END_SRC | |
| #+RESULTS: | |
| [[file:images/python-matplot-fig.png]] | |
| * Resources | |
| - [[http://orgmode.org/org.html][The Org Manual]] | |
| - [[http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-python.html][Python Source Code Blocks in Org Mode]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have an idea how to solve the following problem: http://emacs.stackexchange.com/questions/11075/org-mode-python-session-does-not-return-a-file-name 😊