Emacs Org Mode Snippets Emacs org mode like Jupyter Notebook execution
# Images#+CAPTION: This is the caption for the next figure link (or table)#+NAME: fig:SED-HR4049[[./img/a.jpg]]
# Image with custom width#+attr_html: :width 1000px#+attr_org: :width 1000[[~/images/examples.jpg]]
# Link to a specific section/heading of any level in an org mode document called Golang Tutorial[[file:005-Golang.org::*Golang Tutorial][Golang Tutorial]]
# FootnotesThe Org website [fn:50] now looks a lot better than it used to.[fn:50] The link is: https://orgmode.org
# Specify file variables in first line of file# https://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html,# -*- org-image-actual-width: nil; org-imenu-depth: 5 -*-
# Turn on inline images in Org Mode anywhere in buffer# https://orgmode.org/manual/In_002dbuffer-Settings.html,,#+STARTUP: inlineimagesOther Languages with org-babel like Python, Nu, SQL
Section titled “Other Languages with org-babel like Python, Nu, SQL”# Run a specific program using :shebang# https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-shell.html# For example nushell,,#+begin_src sh :shebang #!/home/user/.nix-profile/bin/nu :results outputls#+end_src
# Run a specific program using :shebang# See https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-shell.html#+begin_src sh :shebang #!/home/user/.nix-profile/bin/nu :results output rawsys host#+end_src
# Python session like Jupyter Note books# Source: https://rgoswami.me/posts/jupyter-orgmode/
# or properties block in a section heading (Preferred)* My Python Notebook,:PROPERTIES:,:header-args: :python /home/user/Code/myproject/.venv/bin/python :session One :results output file :exports both,:END:
# Use Python like normal, note =:results output file :exports both= are required for# either python source blocks or in the header arguments#+begin_src python :results output file :exports both
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]y = [2, 4, 6, 8]
plt.plot(x, y)plt.savefig('output.png')# return filename to org-modeprint('output.png')#+end_src
# or Set the environment Python binary in the properties for the file#+PROPERTY: header-args:python :python /home/user/Code/myproject/.venv/bin/python :session One :results output :exports both#+title: Emacs org mode like Jupyter Notebook execution* Python Session
# Run Python code with uv# https://alexguerard.com/blog/org-and-uv#+begin_src python :results output :python uv run --env-file .env -# /// script# requires-python = ">=3.12"# dependencies = [# "anthropic",# ]# ///import anthropic
client = anthropic.Anthropic()models = client.models.list()
print('\n'.join([x.id for x in models]))#+end_src
# Run Python code with uv with env file#+begin_src python python uv run --env-file .env -# Code#+end_src
# Set Python command, such as a virtual environment to use, #+begin_src emacs-lisp(setq org-babel-python-command "/usr/bin/python3")#+end_src
# Run Python code and show executable being used#+begin_src python :results outputimport sysprint(sys.executable)print("Hello World")#+end_src
# Output results as JSON or other code programming languages#+begin_src sh :results output code :wrap src jsoncurl https://jsonplaceholder.typicode.com/todos/1#+end_src
# Run SQL# https://orgmode.org/worg//org-contrib/babel/languages/ob-doc-sql.html#+begin_src sql :engine mysql DESCRIBE `some_table`;#+end_srcThe following sections shows babel examples and results in org-mode
Python execution
Section titled “Python execution”Python session like Jupyter Notebooks
Section titled “Python session like Jupyter Notebooks”Source: https://rgoswami.me/posts/jupyter-orgmode/
Set the environment Python binary in the properties of the heading
or properties block in a section heading
,* Python Session with Virtual Environment Set (Preferred)
,* Python Session with Virtual Environment Set with directory
or specific at document
After those org-mode property settings, run Python blocks like normal.
Alternative for simple blocks, M-x add-file-local-variable to set the
local varible org-babel-python-command
Running commands
Section titled “Running commands”Use Python like normal, note :results output file :exports both are
required for either python source blocks or in the header arguments
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]y = [2, 4, 6, 8]
plt.plot(x, y)plt.savefig('output.png')# return filename to org-modeprint('output.png')Set Python command, such as a virtual environment to use
(setq org-babel-python-command "/usr/bin/python3")Run Python code and show executable being used
import sysprint(sys.executable)print("Hello World")Run Python code with virtual environment
Set the Python executable as the virtual environment one inside the
.venv folder.
import sysprint("Hello from venv")print(sys.executable)Run Python code with uv
# /// script# requires-python = ">=3.12"# dependencies = [# "matplotlib",# ]# ///import sysprint("Hello from uv")print(sys.executable)Run a specific program using :shebang
Section titled “Run a specific program using :shebang”See https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-shell.html
This method can be used to run any command, programming language and get its output with a specific path or in the user’s PATH environment variable. For example running nushell commands.
sys hostRun shells available on path
ps | where name =~ 'emacs'Using !shebang with Racket, Python, and Deno examples
#lang racket/base(+ 1 1)print("Hello from Python!")import * as R from "npm:ramda"
R.forEach( (i) => console.log(i), [1, 2, 3, 4])Output results as JSON or other code programming languages
Section titled “Output results as JSON or other code programming languages”curl https://jsonplaceholder.typicode.com/todos/1Using hurl for API testing, see Org-mode and Hurl, requires hurl-mode or run commands using :shebang or regular shell
GET https://swapi.dev/api/people/1/
HTTP/2 200Using hurl for passing variables
GET {{hostname}}/api/people/1PlantUML
Section titled “PlantUML”Activate inlineimages in org-mode and output image file and adjust
width for better viewing
@startumlclass Animal { +String name}
class Dog { +bark()}
Animal <|-- Dog@endumlSee more examples at PlantUML Snippets - PlantUML Snippets
Source: SQL Source Code Blocks in Org Mode with examples from Ryan Prior / Emacs Org Data Starter · GitLab
Make sure the corresponding sql binary is installed on the system like sqlite3, psql, and others
DESCRIBE `some_table`;Run R for SQL query
Section titled “Run R for SQL query”library(RMySQL)con <- dbConnect(MySQL(), user="user", password="pwd", dbname="dbname", host="host")q <-<<quote-blks("my-query")>>c <- dbGetQuery(con, q)dbDisconnect(con)cAnother option is the query could be set up to run on its own
SELECT * FROM mytableWHERE id > 500-
Connecting & running a basic query
Follow the MySQL instructions in the org-babel documentation. This example uses source block level properties.
SHOW TABLES; -
Loading data & running queries
Uses subheader properties for the configuration instead of putting the configuration on every block. Use the source block level for one off qureies.
-
Create a database
create database if not exists org_test;
-
PostgreSQL
Section titled “PostgreSQL”-
Connecting & running a basic query
Follow the PostgresQL instructions in the org-babel documentation
\list -
Loading data & running queries using heading properties
-
Create a database
select 'CREATE DATABASE org_test' where not exists (SELECT FROM pg_database WHERE datname = 'org_test')\gexec
-
SQLite
Section titled “SQLite”Follow the ob-sqlite instructions in the org-babel documentation
select sqlite_version();select * from greeting;-
Loading data & running queries at heading level
-
Creating a new table & inserting some data
CREATE TABLE IF NOT EXISTS org_resources (id INT,resource TEXT,url TEXT);
-