avatarEsteban Thilliez

Summary

The web content provides a guide on how to enhance the note-taking application Obsidian with custom Python scripts, making it accessible for users with little to no programming knowledge by leveraging tools like ChatGPT.

Abstract

The article titled "Improve Obsidian with Python" demonstrates how users can extend the functionality of Obsidian, a markdown-based note-taking and knowledge management application, through Python scripting. It emphasizes that even those without programming experience can benefit from these enhancements with the help of ChatGPT, an AI capable of generating code. The guide covers the basics of setting up Python and Git, creating and running simple Python scripts, and using ChatGPT to automate tasks such as listing files within an Obsidian vault. The author has also created a GitHub repository named "obsidian-utils" to share scripts for tasks like text replacement, adding or removing tags, and more. The article concludes with instructions on how to use the scripts, emphasizing the ease of updating the repository with new features.

Opinions

  • The author believes that the lack of certain features in Obsidian can be compensated for by creating custom plugins or scripts.
  • It is suggested that using Python and ChatGPT to automate tasks in Obsidian is both straightforward and time-saving.
  • The author values the sharing of code and collaborative improvement of tools, as evidenced by the creation of the "obsidian-utils" repository.
  • There is an opinion that even complex operations, such as replacing text across an entire Obsidian vault, should be made accessible to non-programmers.
  • The author expresses surprise that some features, like tag removal, are not natively available in Obsidian and thus motivates the development of additional scripts.
  • The article promotes the idea that with the right tools and resources, anyone can customize their software experience regardless of their technical expertise.

Improve Obsidian with Python

Even if you know nothing about programming

Photo by Kevin Canlas on Unsplash

Some very basic but very practical functions are not present by default in Obsidian. I’m thinking, for example, of a feature that lets you replace one text with another across the entire vault or just certain folders.

To achieve this, you can create a plugin. It’s not very complicated, but it’s time-consuming and you need to know a lot about programming.

Fortunately, with Python, this can be done very easily. You don’t even need to create a plugin, you can just create a little script of a few lines. And with ChatGPT, you don’t even need to know how to code to do such simple things.

This article is aimed at everyone, whether you know programming or not. For those who already know, you can skip certain sections, you won’t miss a thing.

Setting Up

So I’m going to start with the basics, i.e. the software to install. You’ll need:

  • Python
  • Git (optional)

Installing this software is very simple. Here’s the Python download page: https://www.python.org/downloads/, and then run the installed executable. For Git, it’s here: https://git-scm.com/downloads. You’ll need to install Git if you want to easily download the scripts I’ll provide at the end of the article. As with Python, all you have to do is launch the executable.

Creating and Running a Script

A script is just a .py file. So, you can start by creating a file named test.py. Then, you just have to add some Python code to it. For example:

print("Hello world!")

Now, if you open a command prompt and type python test.py , it will run your script and show “Hello world!”.

Generating Code Without Coding

Let me introduce my friend: ChatGPT. You probably already know it, so I won’t explain what it is. I’ll just show you how you can use it to generate code.

Basically, you just need to open it (https://chat.openai.com/) and ask him what you want him to generate.

For example, let’s say you want to list all the files in your vault (it’s probably useless, but it’s just for the example):

Here is the code if you want to try it:

import os

def list_files(directory):
    for root, dirs, files in os.walk(directory):
        for file in files:
            file_path = os.path.join(root, file)
            print(file_path)  # You can modify this line to do whatever you want with the file path

# Prompt the user for the directory path
directory_path = input("Enter the directory path: ")

# Call the function to list the files
list_files(directory_path)

You just have to paste this code in your test.py file. Now, let’s run it:

python test.py
Enter the directory path: /run/media/estebanthilliez/Documents/SHARED/3. OBSIDIAN VAULT
...
/run/media/estebanthilliez/Documents/SHARED/3. OBSIDIAN VAULT/90 Meta/Templates/20 Me/Reviews/Monthly Review.md
/run/media/estebanthilliez/Documents/SHARED/3. OBSIDIAN VAULT/90 Meta/Templates/20 Me/Reviews/Weekly Review.md

It works! But as you can see, it displays the full path. We don’t want this, so we just have to say this to ChatGPT and he will fix this:

And now it works!

Obsidian Utils Repo

I’ve created a repository where I plan to store the scripts I use to add some features to Obsidian. You can find it here: https://github.com/estebanthi/obsidian-utils

For programming novices, a repository is just like a folder where you store code, but it can be shared easily and you can track the changes made to your code.

You can download this repository by opening a command prompt and typing the following command:

git clone https://github.com/estebanthi/obsidian-utils

Then, you can just open a command prompt in the obsidian-utils directory and type python name_of_script.py to execute one of my scripts. (although you need to create or modify a config.yaml file before running certain scripts).

You can find more information about the scripts in this repository here: https://github.com/estebanthi/obsidian-utils/blob/main/README.md

For now, I just have 3 scripts:

  • quick_replace.py : to replace text in a folder in your vault
  • add_tag_to_random_notes : to add a tag to n random notes in a folder in vault (I use this with DataView to display 30 random notes from my vault every week)
  • remove_tag : to remove a tag from the notes in a given folder of your vault (I was surprised there isn’t this feature in Obsidian)

If one day I update the repository and add new scripts, you can get the updated version by running the command git pull origin in the folder of the repository (maybe I will leave a comment here every time I update the repo?).

2023–10–21 Update

I’ve modified the code quite a bit in the meantime, and now the steps are a bit different for using the scripts.

Once the repository has been cloned, you need to install the dependencies by running the following command in the repository folder:

pip install -r requirements.txt

Then, you have to create a file and name it config.yaml (if not already present). You have to fill it with the following config options:

vault_path: "/path/to/your/vault"  # for example: "/home/esteban/ObsidianVault"
logging_level: "WARNING"

Then, you can run the following command:

python app.py
Usage: app.py [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  add-tags          Add tags to files
  edit-frontmatter  Edit frontmatter of files
  find-backlinks    Find backlinks in vault
  match-backlinks   Compare existing backlinks with backlinks not created
  remove-tags       Remove tags from files
  replace           Replace a regex in files

It shows the available commands, and how to use them. If you want to know how to use a command, just run:

python app.py command --help

For example:

python app.py add-tags --help

Usage: app.py add-tags [OPTIONS] [DIRECTORY]

  Add tags to files

Options:
  -t, --tag TEXT
  -r, --recursive       Browse directories recursively
  -n, --number INTEGER  Add tags to n random files
  --yes                 Confirm the action without prompting.
  --help                Show this message and exit.

Let’s say you want to add the tag “random” to 30 notes in the directory called “My Notes” in your Obsidian vault, here is the command:

python app.py add-tags -t random -n 30 "My Notes"

You have to confirm:

Are you sure you want to add tags to files in your vault? [y/N]:
y

And it should work!

Final Note

If you can’t create a script you need and want me to do it for you, contact me or leave a comment and I’ll arrange to add it to the repository or send you the code!

I hope you enjoyed this article. My aim was to address all audiences, even those who know nothing about programming, and to explain how it’s still possible to do something even without technical knowledge. But it’s hard to put yourself in the shoes of someone who knows nothing and therefore to explain, so if you need clarification, leave a comment!

Thanks for reading! Here are some links that may interest you:

Obsidian
Productivity
Self Improvement
Python
Notetaking
Recommended from ReadMedium