Skip to content

blakesims/obsidian-python-scripter

 
 

Repository files navigation

Obsidian Python Scripter

Summary

This is a program to quickly allow you to run your own Python scripts from inside Obsidian. To get started enable this plugin, it will create a scripts/python folder inside your designated .obsidian folder. For each script it will add a custom command to the obsidian commands panel.

Requirements

  • Install your preferred version of python and make sure it functions from the command line eg: python .obsidian/scripts/python/main.py
  • This plugin works with naked python scripts eg. scripts/python/example.py
  • Or you may want a more complicated script, you can have a folder as a script. You just need to have a main.py file inside of a src folder for it to function as a command, otherwise it will not be able to execute. Make sure to structure the scripts/python as follows
  scripts
    |
    ---- python
           |
           ---- example
                  |
                  ---- src
                        |
                        ---- main.py
                        |
                        ---- hello.py

Your resulting scripts folder should have a structure like the following:

  scripts
    | 
    ---- python
           | 
           ---- example 
           |       | 
           |       ---- src 
           |             | 
           |             ---- main.py 
           |             | 
           |             ---- hello.py 
           | 
           ---- example2.py 

Obsidian Directions

  • Add your python scripts in the format specified in the requirements section.
  • Once Obsidian starts, your commands will be added to the commands window and can be run as you would like.
  • If Obsidian is already open it will need to be Restarted when first added but not after that
  • In the above example, there would be 2 commands added:
    • Run example
    • Run example2.py

commandwindow

Script Direction

The script passes 2 arguments to the script, argument 1 is the absolute path to the obsidian root folder, argument 2 is the path to the open file if it is open, otherwise an empty path is sent e.g. python \Users\nick\Documents\Vault\.obsidian\scripts\python\main.py \Users\nick\Documents\Vault dir1\file1.md

These can be used by the script as liked.

Here is a starting point for a python script: main.py

# main.py
import sys
import os

python_script = sys.argv[0]
file_path = sys.argv[2]
vault_path = sys.argv[1]

abs_file_path = os.path.abspath(os.path.join(vault_path, file_path))

print(f"This is the open file: {abs_file_path}")

Settings

You have the ability to change the python script location in settings. Additionally you have the ability to change which python executable is used.

Debugging

If your script fails to run. An error is shown in the top right of obsidian and in the developer console which can be found with the following hotkeys: "ctrl" + "shift" + "i" on Windows, or "cmd" + "option" + "i" on MacOS

About

Add the ability to specify a virtual env

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 74.4%
  • JavaScript 23.2%
  • CSS 2.4%