{"id":13577742,"url":"https://github.com/twibiral/obsidian-execute-code","last_synced_at":"2025-04-13T09:00:15.011Z","repository":{"id":37545556,"uuid":"471021194","full_name":"twibiral/obsidian-execute-code","owner":"twibiral","description":"Obsidian Plugin to execute code in a note.","archived":false,"fork":false,"pushed_at":"2024-09-16T17:19:54.000Z","size":4142,"stargazers_count":1136,"open_issues_count":134,"forks_count":67,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-10-29T17:34:38.401Z","etag":null,"topics":["hacktoberfest","javascript","note-taking","obsidian","obsidian-md","obsidian-plugin","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/twibiral.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["twibiral"],"buy_me_a_coffee":"timwibiral"}},"created_at":"2022-03-17T14:21:27.000Z","updated_at":"2024-10-29T04:32:16.000Z","dependencies_parsed_at":"2023-02-16T09:01:13.449Z","dependency_job_id":"1e76cae3-70c6-4b08-b6f5-ebf5d29c5b5b","html_url":"https://github.com/twibiral/obsidian-execute-code","commit_stats":{"total_commits":657,"total_committers":37,"mean_commits":"17.756756756756758","dds":0.680365296803653,"last_synced_commit":"99190a45defcd3c83f29d4d35964c4cc6e3ab9b3"},"previous_names":[],"tags_count":48,"template":false,"template_full_name":"obsidianmd/obsidian-sample-plugin","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twibiral%2Fobsidian-execute-code","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twibiral%2Fobsidian-execute-code/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twibiral%2Fobsidian-execute-code/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twibiral%2Fobsidian-execute-code/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twibiral","download_url":"https://codeload.github.com/twibiral/obsidian-execute-code/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248688571,"owners_count":21145765,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","javascript","note-taking","obsidian","obsidian-md","obsidian-plugin","typescript"],"created_at":"2024-08-01T15:01:23.953Z","updated_at":"2025-04-13T09:00:14.936Z","avatar_url":"https://github.com/twibiral.png","language":"TypeScript","readme":"# Obsidian Execute Code Plugin\r\n\u003cdiv align='right'\u003e\r\n\r\n![Obsidian Downloads](https://img.shields.io/badge/dynamic/json?color=8572db\u0026labelColor=1e1e1e\u0026label=Downloads\u0026query=$['execute-code'].downloads\u0026url=https://raw.githubusercontent.com/obsidianmd/obsidian-releases/master/community-plugin-stats.json) \r\n![GitHub package.json version](https://img.shields.io/github/package-json/version/twibiral/obsidian-execute-code?color=8572db\u0026labelColor=1e1e1e\u0026label=Current%20Version) \r\n![GitHub Release Date](https://img.shields.io/github/release-date/twibiral/obsidian-execute-code?color=8572db\u0026labelColor=1e1e1e\u0026label=Latest%20Release)\r\n\r\n\u003c/div\u003e\r\n\r\nThis plugin allows you to execute code snippets in code blocks in your notes. The plugin adds a 'run' button for code blocks in supported languages. Clicking them results in the code of the block being executed locally. After the execution the result of the execution is shown. An interactive input element is created when your code snippets reads expects user input.\r\n\r\nThe result is shown only after the execution is finished. It is not possible to enter text on the command line into the executed program now.\r\n\r\n![Video that shows how the plugin works.](https://github.com/twibiral/obsidian-execute-code/blob/master/images/execute_code_example.gif?raw=true)\r\n\r\n\r\n\u003chr\u003e\u003c/div\u003e\r\n\r\nThe following [languages are supported](#supported-programming-languages-): C, C++, CSharp, Dart, F#, Golang, Groovy, Haskell, Java, JavaScript, Kotlin, Lean, Lua, Maxima, OCaml, Octave, Prolog, Python, R, Racket, Ruby, Rust, Scala, Shell (including Batch \u0026 Powershell), SQL, TypeScript, Wolfram Mathematica, Zig.\r\n\r\nIf you are new to MarkDown or Obsidian.md, you can go to the [Quickstart Guide](#quickstart-guide-) or take a look in to [some blogs and videos that feature this plugin](#featured-in)\r\n\r\nPython, R, and Octave support embedded plots. All languages support [\"magic\" commands](#magic-commands-) that help you to access paths in obsidian or show images in your notes.\r\n\r\nYou can create code blocks that are executed before or after each code block of the same language and define [global code injections](#global-code-injection-and-reusing-code-blocks-).\r\n\r\nNew: You can enable [persistent output](#persistent-output-experimental) to store the output of a code block in the note and export it to PDF.\r\n\r\nTake a look at the [changelog](CHANGELOG.md) to see what has changed in recent versions.\r\n\r\n[Here](#misc-) you can find some other tools and plugins that are compatible with this plugin and might be useful for you.\r\n\r\n\u003cbr\u003e\r\n\u003cdiv align='center'\u003e\r\n\r\nIf you like this plugin and use it a lot, please consider supporting me in continuing the development of this plugin. You can also sponsor a new feature or language integration directly, if you want to speed up the development for a specific feature you need.\r\n\r\n[![GitHub Sponsors](https://img.shields.io/badge/sponsor-30363D?style=for-the-badge\u0026logo=GitHub-Sponsors\u0026logoColor=white)](https://github.com/sponsors/twibiral)   [![Buy me a coffee](https://img.shields.io/badge/Buy_Me_A_Coffee-FFDD00?style=for-the-badge\u0026logo=buy-me-a-coffee\u0026logoColor=black)](https://www.buymeacoffee.com/timwibiral)\r\n\r\n\u003c/div\u003e\r\n\r\n\r\n## Featured In\r\n\r\n| [![Video by I Versus AI](https://img.youtube.com/vi/eQz4eAW3ZDk/0.jpg)](https://www.youtube.com/watch?v=eQz4eAW3ZDk) | ![![Video by Michel's Science Speedrun](https://www.youtube.com/watch?v=w7vyavrMYqw)](https://img.youtube.com/vi/w7vyavrMYqw/0.jpg) | ![![Video by GlareDB](https://www.youtube.com/watch?v=lRq3mlvcyJw)](https://img.youtube.com/vi/lRq3mlvcyJw/0.jpg) | ![![Video by 노마드 코더 Nomad Coders](https://www.youtube.com/watch?v=qY1P_CmL8aQ)](https://img.youtube.com/vi/qY1P_CmL8aQ/0.jpg) |\r\n|---|---|---|---|\r\n| \"Escape ChatGPT. Make your own Code Interpreter EASY\" by _I Versus AI_ | \"Obsidian \u0026 quarto integration\" by _Michel's Science Speedrun_ | \"Write SQL Queries in...Obsidian?\" by _GlareDB_ | \"인생 노트앱...드디어 찾았습니다..!\" by _노마드 코더 Nomad Coders_|\r\n\r\nIn blogs:\r\n- [\"Why and how you should switch to Obsidian.\" by _Connero_](https://medium.com/@connero_44521/why-and-how-you-should-switch-to-obsidian-c857ae315afd)\r\n- [\"Using Obsidian: Coding Notes\" by _Kera Cudmore_](https://www.codu.co/articles/using-obsidian-coding-notes-pqjyljkh)\r\n- [\"Obsidian and Jupyter Notebooks\" by _Brian Carey_](https://medium.com/@biscotty666/obsidian-and-jupyter-notebooks-5d90ab3eab4c)\r\n\r\n\u003csmall\u003eAre you featuring this plugin in your content? Let me know and I will add it here.\u003c/small\u003e\r\n\r\n\r\n## Supported programming languages 💻\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eJavaScript\u003c/summary\u003e\r\n\r\n- Requirements: Node.js is installed and the correct path is set in the settings.\r\n\r\n```javascript\r\nfunction hello(name) {\r\n\tconsole.log(`Hello ${name}!`);\r\n}\r\n\r\nhello(\"Bob\")\r\n```\r\n\r\n- By default, Javascript runs in Notebook Mode. You can turn this off in the settings.\r\n\r\n\u003c/details\u003e\r\n\t\r\n\u003cdetails\u003e\r\n\u003csummary\u003eTypeScript\u003c/summary\u003e\r\n\r\n- Requirements: Node.js installed then run in command line `npm install typescript -g` and `npm install ts-node -g`. (`-g` means global installation)\r\n- Problems: If you use your global node.js installation, and it doesn't work try to set your `ts-node` path in the settings to `npx ts-node` instead of `ts-node`.\r\n\r\n```ts  \r\nlet message: string = 'Hello, World!';\r\nconsole.log(message);  \r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003ePython\u003c/summary\u003e\r\n\r\n- Requirements: Python is installed and the correct path is set in the settings.\r\n\r\n```python\r\ndef hello(name):\r\n\tprint(\"Hello\", name)\r\n\r\nif __name__ == \"__main__\":\r\n\thello(\"Eve\")\r\n```\r\n\r\n- By default, Python runs in Notebook Mode. You can turn this off in the settings.\r\n- Plots with matplotlib/seaborn are embedded in the note by default. You can turn this off in the settings. Note that plots are only embedded when `plt.show()` is called.\r\n\r\n```python\r\nimport seaborn as sns\r\nimport matplotlib.pyplot as plt\r\nsns.set_style(\"darkgrid\")\r\niris = sns.load_dataset('iris')\r\nsns.FacetGrid(iris, hue =\"species\", height = 5)\r\n\t\t.map(plt.scatter, 'sepal_length', 'petal_length')\r\n\t\t.add_legend()\r\n\r\nplt.show()\r\n```\r\n\r\n![Example of an embedded plot.](https://github.com/twibiral/obsidian-execute-code/blob/master/images/plotting_example.png?raw=true)\r\n\u003c/details\u003e\t\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eR\u003c/summary\u003e\r\n\r\n- Requirements: R is installed and the correct path is set in the settings.\r\n- For cool additional features for R try out [the Ridian plugin](https://github.com/MichelNivard/Ridian)\r\n```r\r\nhello \u003c- function(name){\r\n\tprint(paste(\"Hello\", name, sep = \" \"))\r\n}\r\n\r\nhello(\"Bob\")\r\n```\r\n- Plots can be embedded in the note by default. You can turn this off in the settings.\r\n\r\n```r\r\ny = c(12, 15, 28, 17, 18)\r\nx = 1:length(y)\r\nplot(x, y, type=\"l\")\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eC++\u003c/summary\u003e\r\n\r\n- Requirements: [Cling](https://github.com/root-project/cling) is installed and correct path is set in the settings.\r\n- Code will be executed line-by-line without needing a main function.\r\n\r\n```cpp\r\n#include \u003ciostream\u003e\r\n#include \u003cstring\u003e\r\n\r\nusing namespace std;\r\n\r\nvoid hello(string name) {\r\n\tcout \u003c\u003c \"Hello \" \u003c\u003c name \u003c\u003c \"!\\n\";\r\n}\r\n\r\nhello(\"Alice);\r\n```\r\n\r\n- Main functions can be used as an entrypoint by toggling the option in settings.\r\n\r\n```cpp\r\n#include \u003ciostream\u003e\r\n\r\nvoid main() {\r\n\tstd::cout \u003c\u003c \"Hello, World!\" \u003c\u003c std::endl;\r\n}\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eC\u003c/summary\u003e\r\n\r\n- Requirements: [Cling](https://github.com/root-project/cling) is installed and correct path is set in the settings.\r\n- Code will be executed line-by-line without needing a main function.\r\n\r\n```c\r\n#include \u003cstdio.h\u003e\r\n\r\nprintf(\"Hello, World!\");\r\n```\r\n\r\n- Main functions can be used as an entrypoint by toggling the option in settings.\r\n\r\n```c\r\n#include \u003cstdio.h\u003e\r\n\r\nint main() {\r\n\tprintf(\"Hello, World!\");\r\n\treturn 0;\r\n}\r\n```\r\n\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eJava\u003c/summary\u003e\r\n\r\n- Requirements: Java **11 or higher** is installed and the correct path is set in the settings.\r\n\r\n```java\r\npublic class HelloWorld {\r\n\tpublic static void main(String[] args) {\r\n\t\tSystem.out.println(\"Hello World!\");\r\n\t}\r\n}\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eSQL\u003c/summary\u003e\r\n- Requirements: Have some SQL database installed and the correct path is set in the settings. The default is set to PostgreSQL (`psql`).\r\n- Make sure you adapt the settings to your database (`-d mydatabase -U myuser -f`)The default is set to .\r\n```sql\r\nSELECT * FROM table_name;\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eLaTeX\u003c/summary\u003e\r\n\r\n## LaTeX\r\n\r\nRequirements: LaTeX distribution like [MiKTeX](https://miktex.org/) or [TeX Live](https://www.tug.org/texlive/) is installed and the correct paths are set in the settings.\r\n\r\n### Minimal example\r\n\r\nInjects default document class. Consider setting *Crop to content*.\r\n\r\n```latex\r\n\\begin{document}\r\nHello World!\r\n\\end{document}\r\n```\r\n\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"images/figure_minimal_example.svg\" alt=\"Minimal example\" /\u003e\u003c/p\u003e\r\n\r\n\r\n### Name output file\r\n\r\nSet filename with `\\title{…}`. Adds prefix `figure ` to avoid file name collisions, group all generated files, and for appearance CSS selectors. Click run again will overwrite the file, and refresh its embeddings in the active view.\r\n\r\n```\r\n![[figure time of day.svg]]\r\n```\r\n```latex\r\n\\documentclass[border=2pt]{standalone} \\title{time of day}\r\n\\usepackage{datetime2}\r\n\\begin{document}\r\nThe time is \\DTMcurrenttime.\r\n\\end{document}\r\n```\r\n\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"images/figure_time_of_day.svg\" alt=\"Time of day\" /\u003e\u003c/p\u003e\r\n\r\n### Include attachments\r\n\r\nInclude files relative to the vault's attachment folder. Consider [listings](http://mirrors.ctan.org/macros/latex/contrib/listings/listings.pdf) for source code listings, [markdown](http://mirrors.ctan.org/macros/generic/markdown/markdown.html) for inputting markdown files as LaTeX code, and `\\input{…}` to paste plaintext or LaTeX source files.\r\nLayout with [graphbox](http://mirrors.ctan.org/macros/latex/contrib/graphbox/graphbox.pdf) for vertical alignment, or [tabularray](http://mirrors.ctan.org/macros/latex/contrib/tabularray/tabularray.pdf) for more complex alignment.\r\n\r\n```latex\r\n\\documentclass{standalone} \\title{include_attachments}\r\n\\usepackage{graphicx}\r\n\\begin{document}\r\n\\includegraphics{figure time of day.pdf} \\quad\r\n\\includegraphics{figure time of day.pdf}\r\n\\end{document}\r\n```\r\n\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"images/figure_include_attachments.svg\" alt=\"Include attachments\" /\u003e\u003c/p\u003e\r\n\r\n### Automatically reruns to get cross-references right.\r\n\r\nFor instance reference a label that appears later in the document. The plugin detects `LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.` during compilation and reruns until resolved.\r\n\r\n```latex\r\n\\documentclass{article} \\title{sum of two poisson distribution}\r\n\\usepackage{mathtools,amsfonts}\r\n\\begin{document}\r\nAs seen in \\eqref{eq:poisson}, we use convolutions of probability distributions for two independent poisson distributed random variables.\r\n\\begin{align*} \\MoveEqLeft\r\n\\mathbb{P}(X + Y = k) = \\sum_{m = 0}^\\infty \\mathbb{P}(X = m)\\, \\mathbb{P}(X = m - k) \\tag{1}\\label{eq:poisson} \\\\\u0026\r\n= \\sum_{m = 0}^k \\frac{\\lambda^m\\, e^{-\\lambda}}{m!} \\cdot \\frac{\\mu^{k - m}\\, e^{-\\mu}}{(k - m)!} = \\ldots \r\n\\end{align*} \r\n\\end{document}\r\n```\r\n\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"images/figure_sum_of_two_poisson_distributions.svg\" alt=\"Sum of two poisson distributions\" /\u003e\u003c/p\u003e\r\n\r\nNot all rerun requirements are easy to detect. Consider adding the package [lastpage](http://mirrors.ctan.org/macros/latex/contrib/lastpage/lastpage.pdf) to force a rerun, by creating an unresolved reference.\r\n\r\n```latex\r\n\\documentclass{article} \\title{rerun sidenotes table}\r\n\\usepackage{sidenotes,tabularray,lipsum,lastpage}\r\n\\begin{document} \\SetTblrInner{hlines}\r\n\\sidenotetext{This is a marginal note.} \\lipsum[1][1-3] \r\n\\begin{table*}\r\n\\begin{tblr}[tall, caption={Expand table into page margins}]{X}\r\n    \\lipsum[3][1-4]\r\n\\end{tblr}\r\n\\end{table*}\r\n\\end{document}\r\n```\r\n\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"images/figure_sidenotes_comparison.svg\" alt=\"Sum of two poisson distributions\" /\u003e\u003c/p\u003e\r\n\r\nExplore [more LaTeX examples](https://antonpusch.de/latex), consult [package documentations](https://texdoc.org/), or learn about [LuaLaTeX](http://mirrors.ctan.org/obsolete/info/luatex/lualatex-doc/lualatex-doc.pdf).\r\n\r\n\u003c/details\u003e\r\n\r\n\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eMore supported languages...\u003c/summary\u003e\r\n\u003cdetails\u003e\r\n\u003csummary\u003eCSharp\u003c/summary\u003e\r\n\r\n- Requirements: install dotnet core sdk and run in command line `dotnet tool install -g dotnet-script`, then config dotnet-script fullpath.\r\n\r\n```cs \r\nConsole.WriteLine(\"Hello, World!\");  \r\n```  \r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eDart\u003c/summary\u003e\r\n\r\n- Requirements: dart sdk is installed and the correct path is set in the settings.\r\n\r\n```dart\r\nvoid main() {\r\n  print(\"Hello World\");\r\n}\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eLua\u003c/summary\u003e\r\n\r\n- Requirements: install lua and config lua path.\r\n\r\n```lua\r\nprint('Hello, World!')\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eLean\u003c/summary\u003e\r\n\r\n- Requirements: install lean and config lean path.\r\n\r\n```lean\r\ndef main : IO Unit :=\r\n  IO.println s!\"Hello, World!\"\r\n\r\n#eval main\r\n```\r\n\u003c/details\u003e\r\n\r\n\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eShell\u003c/summary\u003e\r\n\r\n- Requirements: Set the path to your preferred shell in the settings. Default is Bash. (Only on Linux and macOS)\r\n\r\n```shell\r\necho \"Hello World!\"\r\nls -la\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003ePowershell\u003c/summary\u003e\r\n\r\n- Requirements: Used to execute shell commands on Windows. Default is Powershell but can be set to your preferred shell in the settings.\r\n- On macOS: You probably need to change the command to use from `powershell` to `pwsh` in the plugin settings. Make sure you set the right path.\r\n\r\n```powershell\r\necho \"Hello World!\"\r\n```\r\n\r\n- If you prefer batch: change the path settings in the menu for powershell\r\n![Example how to use the magic commands.](https://github.com/twibiral/obsidian-execute-code/blob/master/images/batch_settings.png?raw=true)\r\n\u003c/details\u003e\r\n\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eBatch\u003c/summary\u003e\r\n\r\n- **Requirements**: Used to execute batch commands on Windows (also known as BAT or CMD). Default is command prompt, but can be set to your preferred shell in the settings.\r\n- **Important**: \u003cbr\u003e\r\n\tThe percent sign is used in batch files to represent command line parameters: e.g. %1, %2, ... \u003cbr\u003e\r\n\tTwo percent signs in a batch file are treated like a single percent sign in a command: e.g. %%f \u003cbr\u003e\r\n\tWhen using variables in execute code, use 2 percent signs. More info [here](https://stackoverflow.com/questions/14509652/what-is-the-difference-between-and-in-a-cmd-file)\u003cbr\u003e\r\n\t\r\n```batch\r\nECHO Hello World!\r\n```\r\n\u003c/details\u003e\r\n\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eProlog\u003c/summary\u003e\r\n\r\n- Requirements: NO requirements, works with [Tau-Prolog](https://github.com/tau-prolog/tau-prolog).\r\n- Important: Add your queries after a line \"`% query`\" in the code block like in the following\r\n\r\n```prolog\r\nlikes(john, pizza).\r\nlikes(john, cheese).\r\nlikes(jane, beer).\r\n\r\n% query\r\nlikes(john, X).\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eGroovy\u003c/summary\u003e\r\n\r\n- Requirements: Groovy is installed and the correct path is set in the settings.\r\n\r\n```groovy\r\ndef hello(name){  \r\n\tprintln \"Hello ${name}!\" \r\n}  \r\n\r\ndef helloClosure = {  \r\n\tprintln \"Hello ${it}!\" \r\n}  \r\n  \r\nhello(\"Bob\")\r\n  \r\nhelloClosure \"Bob\"\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eGolang\u003c/summary\u003e\r\n\r\n- Requirements: Golang is installed and correct path is set in the settings(`go` binary is available).\r\n- Every code block must contain package declaration and a main function.\r\n\r\n```go\r\npackage main\r\n\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n\tfmt.Println(\"Hello World\")\r\n}\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eRust\u003c/summary\u003e\r\n\r\n- Requirements: Cargo is installed and correct path is set in the settings(`cargo` binary is available).\r\n- `cargo-eval` is installed. Install using `cargo install cargo-eval`.\r\n- Import statements and external crates is supported by `cargo-eval`. Read\r\n\ttheir [documentation](https://github.com/reitermarkus/cargo-eval).\r\n- Every code block must have a main function.\r\n\r\n```rust\r\nfn main() {\r\n\tprintln!(\"Hello World\");\r\n}\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eKotlin\u003c/summary\u003e\r\n\r\n- Requirements: Kotlin is installed and correct path is set in the settings.\r\n\r\n```kotlin\r\nhello(name: String) {\r\n\tprintln(\"Hello $name!\")\r\n}\r\n\r\nhello(\"Bob\")\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eWolfram Mathematica\u003c/summary\u003e\r\n\r\n- Requirements: Mathematica is installed and correct path is set in the settings.\r\n- You can add `-cloud` as argument in the settings to use the Wolfram Cloud instead of the local installation.\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eHaskell\u003c/summary\u003e\r\n\r\n- You can either use \u003cstrong\u003erunghc\u003c/strong\u003e (compiler) or \u003cstrong\u003eghci\u003c/strong\u003e (interpreter) to run your code.\r\n  - \u003cstrong\u003erunghc\u003c/strong\u003e requirements:\r\n  \t- runghc and ghc are installed and correct paths are set in the settings.\r\n    - Every code block must contain a main function.\r\n  - \u003cstrong\u003eghci\u003c/strong\u003e  requirements:\r\n    - ghci is installed and correct path is set in the settings.\r\n\t- If you have a main function you have to manually call it.\r\n\r\n```haskell\r\nmySum:: Num a =\u003e a -\u003e a -\u003e a\r\nmySum a b = a+b\r\n```\r\n\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eScala\u003c/summary\u003e\r\n\r\n- Requirements: Scala is installed and the correct path is set in the settings.\r\n\r\n```scala\r\nprintln(\"Hello, World!\")\r\n```\r\n\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eRacket\u003c/summary\u003e\r\n\r\n- Requirements: Racket is installed and the correct path is set in the settings.\r\n\r\n```racket\r\n\"Hello, world!\"\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eRuby\u003c/summary\u003e\r\n\r\n- Requirements: Ruby is installed and the correct path is set in the settings.\r\n\r\n```ruby\r\nputs \"Hello, World!\"\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003ePHP\u003c/summary\u003e\r\n\r\n- Requirements: PHP is installed and the correct path is set in the settings.\r\n\r\n```php\r\n\u003c?php\r\necho \"Hello, World!\";\r\n?\u003e\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eOctave\u003c/summary\u003e\r\n\r\n- Requirements: Octave is installed and the correct path is set in the settings.\r\n\r\n```octavia\r\nexp(i*pi)\r\n\r\nx = -10:0.1:10;\r\nplot (x, sin(x));\r\n```\r\n(Thanks to Michael M. Tung for the code example.)\r\n\r\n- Figures are set to invisible by default. They are store in a file and directly embedded in the note.\r\n\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eMaxima\u003c/summary\u003e\r\n\r\n- Requirements: Maxima is installed and the correct path is set in the settings.\r\n\r\n```maxima\r\nintegrate(x,x);\r\nplot2d(sin(x), [x,0,%pi]);\r\n```\r\n(Thanks to Michael M. Tung for the code example.)\r\n\r\n- By default, plots are saved in a file and directly embedded in the note.\r\n\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eOCaml\u003c/summary\u003e\r\n\r\n- Requirements: OCaml is installed and the correct path is set in the settings.\r\n\r\n```ocaml\r\nprint_endline \"Hello, OCaml!\"\r\n```\r\n\u003c/details\u003e\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eSwift\u003c/summary\u003e\r\n\r\n- Requirements: Swift is installed and the correct path is set in the settings.\r\n\r\n```swift\r\nprint(\"Hello, world!\")\r\n```\r\n\u003c/details\u003e\r\n\u003c/details\u003e\r\n\r\nSquiggle: For Squiggle support take a look at the [Obsidian Squiggle plugin](https://github.com/jqhoogland/obsidian-squiggle) by @jqhoogland.\r\n\r\n\r\n\r\n## Magic Commands 🪄\r\n\r\nMagic commands are some meta commands that can be used in the code block. They are processed by the plugin before the source code is executed.\r\n\r\nThe following magic commands are supported:\r\n\r\n- `@vault_path`: Inserts the vault path as string (e.g. \"/User/path/to/vault\")\r\n- `@vault_url`: Inserts the vault url as string. (e.g. \"app://local/path/to/vault\")\r\n- `@note_path`: Inserts the vault path as string (e.g. \"/User/path/to/vault/Note.md\")\r\n- `@note_url`: Inserts the vault url as string. (e.g. \"app://local/path/to/vault/Note.md\")\r\n- `@title`: Inserts the note title as string.\r\n- `@show(ImagePath)`: Displays an image at the given path in the note.\r\n- `@show(ImagePath, Width, Height)`: Displays an image at the given path in the note.\r\n- `@show(ImagePath, Width, Height, Alignment[center|left|right])`: Displays an image at the given path in the note.\r\n- `@html(HtmlSource)`: Displays HTML in the note\r\n- `@content`: Inserts the html content of the note in the code block. ([Here you find a nice example for the usage of this command.](https://github.com/twibiral/obsidian-execute-code/pull/390))\r\n\r\n(`@show(...)` and `@html(...)` are only supported for JavaScript and Python yet.)\r\n(The old commands `@note` and `@vault` are still supported, but may be removed in the future.)\r\n\r\nExamples for the magic commands with Python:\r\n\r\n```python\r\nprint(\"Vault path:\", @vault_path)\r\nprint(\"Vault url:\", @vault_url)\r\n\r\nprint(\"Note path:\", @note_path)\r\nprint(\"Note url:\", @note_url)\r\n\r\nprint(\"Note title:\", @title)\r\n```\r\n\r\n```python\r\n@show(\"image.png\")\r\n@show(\"image.png\", 100, 100)\r\n@show(\"https://upload.wikimedia.org/wikipedia/commons/d/de/TestScreen_square.svg\", 10%, 10%, \"center\")\r\n```\r\n\r\n```python\r\n@html(\"\u003ch1\u003eHTML Caption\u003c/h1\u003e\")\r\n@html('''\r\n\u003csvg width=\"100%\" height=\"100%\" viewBox=\"0 0 600 600\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"\u003e\r\n  \u003ccircle cx=\"300\" cy=\"300\" r=\"250\" style=\"fill:peru;\" /\u003e\r\n  \u003ccircle cx=\"200\" cy=\"250\" r=\"50\" style=\"fill:black;\" /\u003e\r\n  \u003ccircle cx=\"400\" cy=\"250\" r=\"50\" style=\"fill:black;\" /\u003e\r\n  \u003ccircle cx=\"190\" cy=\"230\" r=\"20\" style=\"fill:white;\" /\u003e\r\n  \u003ccircle cx=\"390\" cy=\"230\" r=\"20\" style=\"fill:white;\" /\u003e\r\n  \u003ccircle cx=\"250\" cy=\"400\" r=\"85\" style=\"fill:saddlebrown;\" /\u003e\r\n  \u003ccircle cx=\"350\" cy=\"400\" r=\"85\" style=\"fill:saddlebrown;\" /\u003e\r\n  \u003cellipse cx=\"300\" cy=\"380\" rx=\"50\" ry=\"35\" style=\"fill:black;\" /\u003e\r\n  \u003cellipse cx=\"130\" cy=\"100\" rx=\"110\" ry=\"70\" style=\"fill:saddlebrown;\"/\u003e\r\n\u003cellipse cx=\"470\" cy=\"100\" rx=\"110\" ry=\"70\" style=\"fill:saddlebrown;\" /\u003e\r\n\u003c/svg\u003e \r\n''')\r\n```\r\n\r\nTry it out yourself!\r\n\r\n![Example how to use the magic commands.](https://github.com/twibiral/obsidian-execute-code/blob/master/images/magic_example.png?raw=true)\r\n\r\n## Running in Preview ⏩\r\n\r\nAdding `run-` before the language name in the code blocks (as in the example below) renders the code block in the preview already.\r\nThis allows you to execute the code in the preview.\r\n\r\n``````\r\n```run-python\r\ndef hello(name):\r\nprint(\"Hello\", name)\r\n\r\n    if __name__ == \"__main__\":\r\n        hello(\"Eve\")\r\n`````` \r\n\r\n## Code Block Arguments 🏷\r\n\r\nCode blocks support specifying additional arguments in the form `{key='value', otherkey=['val1', 'val2']}`. Add them to code blocks like so:\r\n\r\n`````\r\n```python {label='my label'}\r\nprint('my labelled code block')\r\n```\r\n`````\r\n\r\n## Global Code Injection and Reusing Code Blocks 📘\r\n\r\nSometimes it is helpful to have code that is executed before or after each other block of the same language. This plugin supports this in a few ways:\r\n\r\n### Global Injection in the Settings\r\n\r\nAll languages have a 'global inject' option in the settings that allows defining code to be added to the top of every single code block on a per-language basis. Code reuse fully works with all languages, and all existing magic commands, including showing images, and inline plot outputs. This can be used to define e.g. often used functions or import your favourite packages or libraries.\r\n\r\n### Note-wide Pre- and Post-Code Blocks\r\n\r\nYou can specify the `pre` argument to create a block that is executed before each following code block:\r\n\r\n``````\r\n```python {pre}\r\nimport pandas as pd\r\n```\r\n``````\r\n\r\nThis code block is added before each python block you define below in the note and import the pandas package.\r\n\r\n`post` blocks work the same way, but the code in post blocks is executed _after_ your other code blocks.\r\n\r\nPre-/post-blocks will only apply to code blocks defined below them, and will only apply to code blocks from the same language.\r\n\r\nYou can also have a pre- and post-block at the same time by specifying `{pre, post}`\r\n\r\nNote, the `pre`/`post` arguments are special in that you don't need to explicitly state a key/value pair, however you can do so if you wish:\r\n\r\n`{pre}` is equivalent to `{export='pre'}`, `{pre, post}` is equivalent to `{export=['pre', 'post']}`.\r\n\r\n### Labelled Code Blocks\r\n\r\nYou can label specific code blocks with the `label='string'` argument, then import them explicitly in other blocks with the `import='string'` or `import=['string1', 'string2', ...]` argument so they aren't automatically imported as with pre-/post-blocks:\r\n\r\n`````\r\n```python {label='block 1'}\r\nprint('running block 1')\r\n```\r\n\r\n```python {label='block 2'}\r\nprint('running block 2')\r\n```\r\n\r\n```python {import=['block 1', 'block 2']}\r\nprint('should run block 1 and 2')\r\n```\r\n`````\r\n\r\nLabelled code blocks will be executed before the code block being run, however after global injects and pre blocks.\r\n\r\n### Ignoring Code Exports\r\n\r\nIn case you want to manually ignore specific exports in a code block like pre / post / global exports, you can do so with the `ignore` argument that accepts either `pre`, `post`, `global`, an array of any of these 3, or `all` to ignore all exports:\r\n\r\n`````\r\n```python {ignore='all'}\r\nprint('should not run any global injects or pre / post blocks')\r\n```\r\n\r\n```python {ignore=['global', 'pre']}\r\nprint('should not run any pre blocks or global injects')\r\n```\r\n`````\r\n\r\n### Notebook Mode\r\n\r\nA few languages (currently JS and Python) support *Notebook Mode*. If a language is using Notebook Mode (configurable in Settings), then all code blocks in a given file will execute in the same environment.\r\n\r\nVariables functions, etc. defined in one code block will be available in other code blocks. Code blocks are executed on demand; the order of code blocks in the file does not affect the order in which they are executed:\r\n\r\n``````\r\n```js\r\nconsole.log(f)\r\n```\r\n```js\r\nvar f = 3;\r\n```\r\n``````\r\n\r\nRunning the first code block, then the second, then the first again will give:\r\n\r\n```\r\nUncaught ReferenceError: f is not defined\r\nundefined\r\n3\r\n```\r\n\r\nTo manage the open runtimes for Notebook Mode, you can use the `Open Code Runtime Management` command in the command palette. From this sidebar window, you can stop kernels. **Note: force-stopping requires `taskkill` on Windows and `pkill` on Unix. 99% of systems should have these preinstalled: if yours doesn't, please [file an issue](https://github.com/twibiral/obsidian-execute-code/issues/new/choose)**\r\n\r\n\r\n### Persistent Output \\[Experimental\\]\r\n\r\nSince version 2.0.0, the plugin supports persistent output. This means that the output of a code block is \r\nstored in the note and will be displayed when you open the note again. This is useful for long-running code blocks or \r\ncode blocks that produce a lot of output. The output is stored in the note as a comment and will be displayed in the \r\npreview mode.\r\n\r\nTo enable this feature, you have to enable the setting `Persistent Output` in the plugin settings.\r\nWe recommend reopening open notes that contain code blocks after enabling this feature.\r\n\r\n⚠ This feature is still experimental and may not work as expected in all cases!\r\nWe recommend that you disable this feature if you encounter any problems.\r\n\r\n\r\n## Misc 📦\r\n### Style Settings 🎨\r\n\r\nThis plugin supports customising styles using the [Style Settings plugin](https://github.com/mgmeyers/obsidian-style-settings) \r\nor the [Obsidian Code Styler plugin](https://github.com/mayurankv/Obsidian-Code-Styler).\r\n\r\n### Other Tools\r\n\r\nTake a look at the [Obsidian Tools python package](https://github.com/mfarragher/obsidiantools) to find some useful \r\ntools for interacting with your vault.\r\n\r\n\r\n## Quickstart Guide\r\n\r\nStart by checking if the plugin is installed and **activated**. Continue by creating a code block in your preferred language. Check above to see if the language is supported.\r\n\r\nNow you can switch from the preview mode to the rendered mode (where you can't edit the text anymore). Now, when you hover over the code block, you should see a button labeled \"run\". Click it!\r\n\r\nIf it didn't work and a warning appears (that is not related to a bug in your code), you probably need to set the correct execution part for the language (e.g., \"C://User/YourName/anaconda/python.exe\" or something like that). You can find instructions about how to find the right path below.\r\n\r\nNow it works, great! Feel free to read or skim through the text above to learn more features you might like, for example persistent output or embedding of plots.\r\n\r\n\r\n## Installation 💾\r\n\r\nIn your vault go to Settings \u003e Community plugins \u003e Browse and search for \"Execute Code\". Select the plugin, install it and activate it.\r\n\r\nor\r\n\r\nFollow [this link](https://obsidian.md/plugins?search=execute%20code#) and click \"Open in Obsidian\".\r\n\r\n\r\n## Locating Path Settings ( ex. JavaScript | Node )\r\n\t\r\nTo avoid or resolve errors from an incorrect path.\r\n\t\r\n('where' for Mac and Windows) --- (for Linux Users, replace 'where' with 'which')\r\n\t\r\n1. In your terminal, type 'where node'\r\n   ![Type 'where node' in terminal](https://github.com/twibiral/obsidian-execute-code/blob/master/images/path_location_shell.png?raw=true)\r\n2. Copy path from terminal ( ex. /opt/homebrew/bin/node )\r\n3. Paste in path under settings ( ex. Node path )\r\n   ![Update path under settings with path from step 2](https://github.com/twibiral/obsidian-execute-code/blob/master/images/path_location_settings.png?raw=true)\r\n\r\n\r\n## Warning ⚠\r\n\r\nDo not execute code from sources you don't know or code you don't understand. Executing code can cause irreparable damage.\r\n\r\n## Known Problems 🛠\r\n\r\n- On Linux, Snap/Flatpak/AppImage installations of Obsidian run in an isolated environment. As such, they will not have access to any of your installed programs. If you are on Linux, make sure to install the `.deb` version of Obsidian. If your distro isn't compatible with `.deb` files, you may see issues.\r\n- Missing when `run` button after switching the theme: Try to close and reopen your notes and wait for a few minutes. It seems like obsidian doesn't call the postprocessors after the theme switch.\r\n- Pre-/Post-blocks may not be executed if the file contains duplicate code blocks.\r\n- In Python, Embed Plots may not be off while Notebook Mode is on\r\n\r\n## Contribution 🤝\r\n\r\nAll contributions are welcome. Just create a merge request or email me: contact(at)tim-wibiral.de\r\n\r\nThe [open issues](https://github.com/twibiral/obsidian-execute-code/issues) are a good starting point to find something to work on. Some are marked as [\"good first issue\"](https://github.com/twibiral/obsidian-execute-code/labels/good%20first%20issue) and are easier to solve.\r\n\r\n## Contributors ♥\r\n\r\n\u003ca href=\"https://github.com/twibiral/obsidian-execute-code/graphs/contributors\"\u003e\r\n  \u003cimg alt=\"List of contributors to this project.\" src=\"https://contrib.rocks/image?repo=twibiral/obsidian-execute-code\" /\u003e\r\n\u003c/a\u003e\r\n\r\n\u003csub\u003eMade with [contrib.rocks](https://contrib.rocks).\u003c/sub\u003e\r\n","funding_links":["https://github.com/sponsors/twibiral","https://buymeacoffee.com/timwibiral","https://www.buymeacoffee.com/timwibiral"],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwibiral%2Fobsidian-execute-code","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwibiral%2Fobsidian-execute-code","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwibiral%2Fobsidian-execute-code/lists"}