Canvas Blocks
by Kay606
Score: 53/100
favorite
share
Description

The Canvas Blocks plugin integrates Python scripting capabilities with Obsidian Canvas, allowing users to automate tasks such as generating QR codes, modifying images, and creating custom workflows. Users can work with simple scripts for quick execution or design complex workflows that chain multiple scripts for repeated tasks. The plugin supports real-time interactivity within the Canvas, where scripts can receive inputs from other nodes and generate outputs. It also allows for API key handling and provides a library of useful functions for managing data. The plugin is highly customizable, letting users define their own prompts and actions, making it a powerful tool for integrating automation into their notes.

Stats
27
stars
1,271
downloads
0
forks
329
days
159
days
264
days
1
total PRs
0
open PRs
0
closed PRs
1
merged PRs
2
total issues
0
open issues
2
closed issues
114
commits
Latest Version
9 months ago
Changelog
README file from

GitHub Release
Obsidian Downloads

Canvas Blocks

Canvas Blocks allows you to integrate python snippets with Obsidian Canvas.

There are 2 modes for this plugin: Simple and Workflow

Workflow scripts are recommended for new users as the inputs required are intuitive but they take more effort to set up.

Simple scripts take a maximum of 1 optional input as well as optional settings processes it. It allows scripts to be very quickly executed with an input. Examples include generating a QR code from text, making an image grayscale and rotating an image.

Workflow allows more complex usage by chaining together multiple scripts. This is useful for repeated use for more complex processing. All of the simple scripts can also me made into workflow scripts but it is slightly slower to run.

It is recommended that you set a hotkey for Canvas Blocks: Execute canvas script as you will need to execute this command every time you execute a script

The python version used by the plugin will be automatically detected. This can be overridden using the Python path setting (example: F:\Program Files\Python310\python). The path must end in the python executable name (python, python.exe, python3 or python3.exe)

Usage

Simple

  • Copy the text of one of the examples into a canvas text node or copy it into a markdown file and drag the file into a canvas
  • Select a script node and run the command Canvas Blocks: Execute canvas script to execute it
  • Scripts can be given a parameter by dragging another node over the script node before executing it. The script will then use this to process
  • For some scripts, edges can be used for additional parameters by pointing the arrow into the script node
  • Some of these example scripts may output data into the plugin's data folder which can be altered in settings (default is the vault's root directory). This folder must be manually created for the scripts to work correctly if it is changed from the default

Refer to the Simple Demonstrations heading in this readme for more information

Workflow

  • Go to the plugin's settings and set the Workflow script folder. You should then create this folder in your vault
  • Copy one of the workflow examples into the Workflow script folder folder. Each script will have 2 code blocks in it. Both are necessary
  • Run the Canvas Blocks: Add workflow script command and select the script which you want to add. This is the only way to add scripts to the canvas. If you copy and paste a script, it will not work correctly. If you accidentally delete part of a script, delete the rest of it and add the script back
  • Connect all required inputs
  • Select the group (the gray box) of the script to run
  • Run the Canvas Blocks: Execute canvas script command to execute the script. This will run the script selected and all that are connected by its inputs. It will not execute any connected to the outputs. If the outputs are used, select the script that uses it
  • If you use some of the workflow examples, you may see no change as it outputs data rather than displaying it. You may need to attach further nodes such as Save Image to see the outputs

It is recommended that you set a hotkey for Canvas Blocks: Add workflow script as you will need to execute this command every time you add a workflow script to the canvas

Refer to the Workflow Demonstrations heading in this readme for more information

Demonstrations

Simple

QRCodeExample

ResizeExample

Workflow

image

Writing Scripts

You can write your own scripts for either the Simple or Workflow usage of the plugin or use the premade scripts: Simple examples or Workflow examples

If you want to request a script to be made, create an issue and mark it with the script request tag. Please fully describe the usage of the script, state whether you want a Simple or Workflow script and if possible, give an example usage

Library

The plugin contains a library for useful functions to interface with the canvas such as these and more

  • install_dependency Checks if a python module is installed and installs it with pip if it is not
  • create_text_node Creates a text node in the canvas
  • create_file_node Creates a file node in the canvas

For more information on this, read the provided library at Canvas Blocks Python library. All functions provided are well documented.

Simple

The plugin exposes several variables to the script to allow it to process parameters. All node data is provided in the JSON Canvas format used by Obsidian Canvas in the form of python objects

  • script_data A copy of the script being executed is provided
  • parameter_data The node data of the parameter node (the node which overlaps with the script in the canvas). If none are overlapping, this will be {}
  • arrow_parameters An list containing all nodes with an edge pointing into the script
  • vault_path The absolute path to the root directory of the vault
  • plugin_folder A relative path from the vault root. This is set in the settings but defaults to Assets/CanvasBlocks. This can be used for storing data generated by scripts
  • canvas_path A relative path from the vault root to the canvas file which the script is running from
  • has_parameter A boolean value for whether there is a parameter node (only considers overlapping nodes, not ones linked by edges)

Workflow

Each workflow script must contain a canvasblocksettings and pycanvasblock code block

The canvasblocksettings must be JSON in the following format:

{
    "ioConnections":
    {
        "YOUR_CONNECTION_NAME": {
            "direction": "input|output",
            "type": "any|image|text|file|integer|float|YOUR_DATA_TYPE"
        }
    }
}

There can be multiple connections, more can be added by creating a new entry in the ioConnections dictionary where the key is the connection name. In the above example, YOUR_CONNECTION_NAME can be any name and the type can be any of the shown types or your own

See the Workflow examples for more examples on how to structure the canvasblocksettings block


The pycanvasblock block contains the python code which will be ran. The following is the data provided to the script as variables which can be used for processing

  • in_data This is the input data to the workflow script from the previous scripts/nodes which are connected as inputs
  • out_data This is the output data from the workflow script to the next scripts which use it as inputs
  • script_settings This will be a copy of the canvasblocksettings code block from the script structured as a python dictionary and not as a string
  • script_data A copy of the script node which is being executed is provided (this will be a string containing both code blocks)
  • vault_path The absolute path to the root directory of the vault
  • plugin_folder A local path from the vault root. This is set in the settings but defaults to Assets/CanvasBlocks. This can be used for storing data generated by scripts
  • canvas_path A relative path from the vault root to the canvas file which the script is running from

Handling inputs and outputs

The in_data variable will contain a dictionary. The key to the dictionary will be the name of the connection specified in the canvasblocksettings code block. The value will depend on the type specified in the connection.

Example: print(in_data["Text connection name"])

  • image: A PIL object containing an image
  • text: A Python str
  • file: A dictionary of a node in the JSON Canvas format
  • integer: A Python int
  • float: A python float
  • YOUR_DATA_TYPE: Any JSON serializable object

To output data you must set the value on the out_data dictionary. The key will be the name of the connection specified in the canvasblocksettings code block and the value also will depend on the type specified in the connection.

You don't need to attempt to serialize the data before setting the value in the dictionary. This means that PIL images can be set directly without needing to save the image as bytes/base64 string
Example: out_data["Text connection name"] = "Hello world!"

Example: out_data["Image connection name"] = Image.new('RGB', (100, 100))

Handling API Keys

API Keys, tokens or any other data can be stored in one of the "variables". This can be accessed in the settings
image

Add a new variable and set the name and value of it. This name must match the name used in the scripts. Certain example scripts such as Send Discord Image will have a specific name required, for this example it is discord_token for the bot's token

To access this in a script, you must grant intents to the script by setting allowedVariables in the canvasblocksettings code block. This will work in Simple and Workflow scripts. The value of this setting must be a list of strings where each string is the name of the variable

An example of this from Send Discord Image

{
    "type": "workflow",
    "ioConnections": {
        "Data": {
            "direction": "input",
            "type": "file"
        },
        "Channel ID": {
            "direction": "input",
            "type": "integer"
        }
    },
    "allowedVariables": ["discord_token"]
}

This allows the script to access the variable. To use it in python, you must access the injected_variables dictionary such as token = injected_variables["discord_token"]. This will always return a string with the value set in the settings

Installation

Install from the Obsidian Comunity Plugins tab or Canvas Blocks

You can manually install by adding Kay607/obsidian-canvasblocks to BRAT

After enabling the plugin, close and reopen all canvas files which use this script

Contribution

Feel free to create an issue or pull request

If you write a script which may be useful to others, you can create a pull request to have it added to the repository

Building

Requires npm to be installed

  • git clone https://github.com/Kay607/obsidian-canvasblocks --recursive Clone the repository into the .obsidian/plugins folder
  • npm i Install modules
  • npm run dev Builds the plugin when a change is made
Similar Plugins
info
• Similar plugins are suggested based on the common tags between the plugins.
Sequence Hotkeys
3 years ago by Ruan Moolman
Obsidian plugin to support a sequenced of keyboard shortcuts to run commands.
Open File by Magic Date
3 years ago by simplgy
Auto Hide
3 years ago by skelato1
This Obsidian plugin enables you to collapse (and expand) the sidebar easier.
ToggleList
2 years ago by Lite C
This is a simple plugin for Obsidian to overwrite the default behavior of toggle checkbox status. Also, it offers a simple way to toggle through frequently used attributes: task states, task tags, highlighted list, etc.
Rapid Notes
2 years ago by valteriomon
File chucker
2 years ago by Ken Lim
Task Marker
2 years ago by wenlzhang
An Obsidian plugin to change task status and append text with hotkeys and right-click context menu.
Weekly Review
2 years ago by Brandon Boswell
Vim Toggle
2 years ago by Conner Ohnesorge
This is a plugin that adds the ability to toggle on and off vim into obsidian with a nice notice to let you know when you switched.
Custom State for Task List
2 years ago by Okami Wong
A plugin for Obsidian to define your own states for task items.
GTD No Next Step
2 years ago by Tobias Davis
Obsidian plugin for GTD workflow, badge projects with no next step.
Swiss army knife
2 years ago by mwoz123
ZettelFlow
2 years ago by RafaelGB
ZettelFlow is a plugin for Obsidian that helps you to create and manage your notes in a Zettelkasten way.
Next Link
a year ago by Juan Luque
Tag Project
a year ago by Odaimoko
Editor Autofocus
a year ago by Mgussekloo
Tag Buddy
a year ago by David Fasullo
Unlock powerful tag editing features in Reading Mode. Add, remove and edit tags across your vault. Use tag inboxes to level up any workflow with a powerful idea assembly line.
Slash Commander
a year ago by alephpiece
Customizable slash command list for Obsidian.md
Custom save
a year ago by Hananoshika Yomaru
add custom save action to your save command
open-as-md
a year ago by kursad-k
open and edit preferred formats as markdown in Obsidian
Task Status
a year ago by Valerie Burzynski
Quickly change any task status in Obsidian. The searchable modal enables a more dynamic and indiscriminate workflow when you need to change your checkbox markers.
Extended Task Lists
a year ago by joeriddles
Extended Markdown support for task lists in Obsidian.
Templated daily notes
a year ago by digitorum
Allow to create templayted daily note in specific folder
New Tab +
a year ago by Raphaël Le Carval
Allow to open markdown files, graph and canvas in new tab as the default behavior.
Notice logger
a year ago by @gapmiss
An Obsidian.md plugin for logging all notices to the developer console, with optional prefix & timestamp.
Alias Picker
a year ago by rostunic
Personal OS
a year ago by A.Buot
Outline to task list
a year ago by alexandrerbb
A simple Obsidian plugin to convert a note's outline to a task list
LinkMagic
a year ago by AndyReifman
Note Chain
a year ago by ZigHolding
Package my frequently used tools, highly personal plugins.
Close Window When Empty
10 months ago by Taylor Jadin
Quick Open
8 months ago by James Alexandre
Quickly select items in any modal using keyboard shortcuts. Supercharge your workflow with fast, efficient item selection in Obsidian modals.
Custom window title
4 years ago by Joost Plattel
Small Obsidian plugin that updates the window title with the current open note
Hotkeys for specific files
4 years ago by Vinzent
Homepage
4 years ago by mirnovov
An Obsidian plugin that opens a specified note, canvas, or workspace on startup, instead of the most recent one.
Open with
4 years ago by phibr0
Command Alias
4 years ago by @Yajamon
Obsidianのコマンドに対してエイリアスを設定するプラグイン
Archiver
4 years ago by ivan-lednev
Archive completed tasks in your Obsidian vault (plus other org-mode-like features)
Overdue
3 years ago by Peter Parente
Obsidian plugin that marks items as [[Overdue]] if they are not checked off by their due date
Peekaboo
5 months ago by Wang Guoshi
An Obsidian plugin protects your privacy by setting a password to hide notes.
Bottom to Top
5 months ago by Henry Gustafson
BlazeJump
5 months ago by henryco
Plugin for Obsidian that makes text navigation blazingly fast.
NodeFlow
3 months ago by LincZero
Render node streams like `ComfyUi`, `UE`, `Houdini`, `Blender`, etc., to make it easy to write relevant notes. json describes the chart, compared to screenshots, making it easier to modify later. The plugin is also compatible with blogs.",
Better Plugins Manager
3 months ago by zero
Context Command Hider
2 months ago by Mara-Li
This plugin allows you to hide every command from the Obsidian's right-click context menu.
Spacekeys
2 months ago by Jared Lumpe
Obsidian plugin to define hotkeys based on sequences of keypresses.
New Note Fixer
2 months ago by mnaoumov
Obsidian Plugin that unifies the way non-existing notes are created when clicking on their links
Runsh
a month ago by Ddone
A simple plugin that allows to run shell commands from obsidian.
Task Board
a month ago by Atmanand Gauns
An Obsidian plugin to view and manage your tasks from whole vault in a much efficient Kanban board format.
Hide Commands in Menu
a month ago by bomian98
Obsidian Plugin, hide different commands in different menus.
Task Mover
a month ago by Mariia Nebesnaia
An plugin for obsidian to move unfinished tasks to the daily note automatically
Cursor Position History
a month ago by Florian Gubler
A Plugin to remember (and make accessible) the cursor history in Obsidian. Both within a file and across files.
Asana
a month ago by Ryan Bantz
Obsidan plugin that creates tasks in Asana for highlighted text or the current line
Previous Daily Note
22 days ago by Marcos Talau
Plugin for Obsidian that opens the previous daily note
URI Converter
21 days ago by wenlzhang
An Obsidian plugin to convert Obsidian URIs to Obsidian internal links.