State & Config
karcytics_sdk.plugin.state
Plugin state management for Karcytics SDK.
Provides base class for plugin state that integrates with Karcytics's undo/redo history system. Enables serialization and deserialization of plugin states for workflow persistence.
PluginState
dataclass
Bases: ABC
Base state class for plugin analysis state.
Subclass this in your plugin and use @dataclass for automatic serialization. Enables undo/redo integration via Karcytics's HistoryManager.
All fields in your state should be simple types (str, int, float, list, dict) to ensure proper serialization. Complex objects should be stored as paths or serializable representations.
This class strictly prevents dynamic attribute assignment. All attributes must be declared as fields in the subclass dataclass.
Source code in src/karcytics_sdk/plugin/state.py
from_dict(data)
classmethod
Reconstruct state from dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Dictionary previously produced by to_dict() |
required |
Returns:
| Type | Description |
|---|---|
PluginState
|
New instance of this state class |
Source code in src/karcytics_sdk/plugin/state.py
to_dict()
Convert state to dictionary for serialization.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary representation of the state |
karcytics_sdk.plugin.io
File I/O utilities for Karcytics SDK.
Provides convenient wrappers for JSON serialization and configuration management for plugins.
PluginConfig
Bases: PreferenceManagerProtocol
Simple configuration management for plugins.
Stores settings in JSON in ~/.karcytics/plugin_configs/{plugin_id}.json
This is useful for persisting user settings across sessions, like the last used parameters or paths.
One instance per plugin_id per process (see __new__): two independent
PluginConfig objects for the same plugin would each cache their own
snapshot of the JSON file in .data, and whichever calls .save() last
would silently overwrite the other's unsaved changes with its own stale
snapshot. A plugin's own config wrapper (e.g. a FlowConfig holding
PluginConfig("flow_cytometry")) and any SDK feature that also reaches
for PluginConfig(same_plugin_id) — the workflow-autosave preference,
for instance — now always share the exact same object and .data dict.
Example
config = PluginConfig('my_plugin') config.set('threshold', 0.5) config.set('last_image_dir', '/path/to/images') threshold = config.get('threshold', default=0.0) config.save()
Source code in src/karcytics_sdk/plugin/io.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
__getitem__(key)
__init__(plugin_id)
Initialize config manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plugin_id
|
str
|
Unique plugin identifier (used for filename) |
required |
Source code in src/karcytics_sdk/plugin/io.py
__setitem__(key, value)
clear()
get(key, default=None)
Get a configuration value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Configuration key |
required |
default
|
Any
|
Value to return if key doesn't exist |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Configuration value or default |
Source code in src/karcytics_sdk/plugin/io.py
has(key)
Check if a key exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Configuration key |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if key exists |
load()
Load config from disk.
Called automatically in init. Call again to reload from disk.
Source code in src/karcytics_sdk/plugin/io.py
save()
Save config to disk.
Raises:
| Type | Description |
|---|---|
IOError
|
If file cannot be written |
Source code in src/karcytics_sdk/plugin/io.py
set(key, value)
Set a configuration value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Configuration key |
required |
value
|
Any
|
Configuration value (should be JSON-serializable) |
required |
get_plugin_logger(plugin_id)
Get a logger for a plugin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plugin_id
|
str
|
Plugin identifier |
required |
Returns:
| Type | Description |
|---|---|
Logger
|
Configured logger with plugin name prefixed to 'karcytics.plugins' |
Example
logger = get_plugin_logger("my_plugin") logger.info("Plugin initialized") # Logs to "karcytics.plugins.my_plugin"
Source code in src/karcytics_sdk/plugin/io.py
load_json(path)
Load JSON from file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Parsed JSON dictionary |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
JSONDecodeError
|
If JSON is invalid |
Source code in src/karcytics_sdk/plugin/io.py
read_binary(path)
save_json(path, data, pretty=True)
Save JSON to file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path |
required |
data
|
dict[str, Any]
|
Dictionary to save |
required |
pretty
|
bool
|
If True, indent for readability (2 spaces) |
True
|
Source code in src/karcytics_sdk/plugin/io.py
write_binary(path, data)
karcytics_sdk.plugin.preferences
Preference Manager Interface for Karcytics SDK.
PreferenceManagerProtocol
Bases: Protocol
Standardized interface for preference management.
This interface ensures that both the Core application and SDK plugins have a unified way to interact with preferences, adhering to the Dependency Inversion Principle (DIP).