Kay607 / obsidian-canvasblocks

MIT License
11 stars 0 forks source link
canvas

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

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

Workflow

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

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

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

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"])

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