-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
230 lines (208 loc) · 7.49 KB
/
main.js
File metadata and controls
230 lines (208 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const { Plugin } = require("obsidian");
const { exec } = require("child_process");
const path = require("path");
const fs = require("fs");
/**
* Helper to get computed CSS variables from the Obsidian theme
* @param {string} varName - the name of the CSS variable (e.g., "--text-normal")
* @returns {string} - the computed color value (e.g., "rgb(255, 255, 255)")
*/
const getCSS = (varName) => {
const temp = document.body.createEl("div", {
attr: { style: `display:none; color: var(${varName});` },
});
const computedColor = getComputedStyle(temp).color;
temp.remove();
return computedColor || "#000000";
};
/**
* Local Python Runner Obsidian Plugin
* - executes Python code blocks using a local venv
* - renders Plotly outputs in iframes
* - provides error handling and user feedback
*/
module.exports = class LocalPythonRunner extends Plugin {
async onload() {
// Check if Python exists on startup
const pythonPath = this.getPythonPath();
if (!fs.existsSync(pythonPath)) {
new Notice(
"Local Python Runner: Python VENV not found at " + pythonPath,
5000,
);
}
// Auto-Refresh Logic (Debounced)
this.setupAutoRefresh();
/**
* registerMarkdownCodeBlockProcessor is a built-in Obsidian API that allows
* us to define custom rendering logic for code blocks with a specific language identifier
*/
this.registerMarkdownCodeBlockProcessor("local-py", (source, el, ctx) => {
this.renderPythonBlock(source, el);
});
// Command: Insert Skeleton
this.addCommand({
id: "insert-local-py-skeleton",
name: "Insert Local-Py Skeleton",
editorCallback: async (editor) => {
const skeleton = await this.getFileContent("skeleton.py");
editor.replaceSelection(`\`\`\`local-py\n${skeleton}\n\`\`\``);
},
});
}
/**
* Watches for theme changes and triggers all visible "Run" buttons
*/
setupAutoRefresh() {
let debounceTimer;
this.registerEvent(
this.app.workspace.on("css-change", () => {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
const runButtons = document.querySelectorAll(".local-py-run-button");
runButtons.forEach((btn, index) => {
setTimeout(() => btn.click(), index * 100);
});
}, 500);
}),
);
}
/**
* Safely reads a file from the plugin's directory
* @param {string} fileName - name of the file to read (e.g., "wrapper.py")
* @returns {Promise<string>} - file content or error message
*/
async getFileContent(fileName) {
try {
const pluginDir =
this.app.vault.configDir + "/plugins/" + this.manifest.id;
const adapter = this.app.vault.adapter;
return await adapter.read(pluginDir + "/" + fileName);
} catch (e) {
console.error(`Local Python Runner: Failed to load ${fileName}`, e);
return "# Error: Could not load " + fileName;
}
}
/**
* Creates "Run Code" button and output area
* Executes Python code in a temp file and handles output/errors
* @param {string} source - the Python code from the markdown block
* @param {HTMLElement} el - the container element for the code block
*/
async renderPythonBlock(source, el) {
// CONTAINER for button and output
const container = el.createEl("div", { cls: "python-runner-container" });
// RUN BUTTON to execute the code block
const button = container.createEl("button", {
text: "Run Code",
cls: "local-py-run-button",
});
// OUTPUT AREA for results or errors
const outputArea = container.createEl("div", { cls: "python-output-area" });
/**
* Event listener for the "Run Code" button
*/
button.addEventListener("click", async () => {
outputArea.empty();
const status = outputArea.createEl("p", {
text: "Generating local-py output...",
});
// Create unique temp file paths to avoid conflicts
const id = Math.random().toString(36).substring(2, 9);
const vaultPath = this.app.vault.adapter.getBasePath();
const pyPath = path.join(vaultPath, "Scripts", "Temp", `temp_${id}.py`);
const htmlPath = path.join(
vaultPath,
"Scripts",
"Temp",
`temp_${id}.html`,
);
// Path to venv python
const pythonExe = path.join(
vaultPath,
"Scripts",
".venv",
"bin",
"python3",
);
// Load wrapper template and inject variables
// {{VARNAME}} placeholders in wrapper.py will be replaced with actual values
let wrapper = await this.getFileContent("wrapper.py");
const fullCode = wrapper
.replace(/{{SOURCE}}/g, source)
.replace(/{{TEXT_COLOR}}/g, getCSS("--text-normal"))
.replace(/{{HTML_PATH}}/g, htmlPath.replace(/\\/g, "/"));
// Write temp file
try {
fs.writeFileSync(pyPath, fullCode);
} catch (e) {
this.showError(outputArea, `Failed to write temp file: ${e.message}`);
return;
}
// Execute the Python script
exec(`"${pythonExe}" "${pyPath}"`, (error, stdout, stderr) => {
status.remove();
// ERROR HANDLING
if (error) { // system level errors
this.showError(outputArea, `System Error: ${error.message}`);
return;
}
if(stderr) { // Python runtime errors
this.showError(outputArea, `Python Error: ${stderr}`);
return;
}
if (stdout.includes("Plot success!")) {
const htmlContent = fs.readFileSync(htmlPath, "utf8");
this.renderIframe(outputArea, htmlContent);
}
// Cleanup temp files after a short delay
setTimeout(() => {
[pyPath, htmlPath].forEach((p) => {
if (fs.existsSync(p)) fs.unlinkSync(p);
});
}, 1000);
});
});
}
/**
* Renders the Plotly HTML inside a seamless iframe
*/
renderIframe(parent, htmlContent) {
const iframe = parent.createEl("iframe", {
attr: {
srcdoc: htmlContent,
style: `width: 100%; height: 0px; border: none; border-radius: 10px; visibility: hidden; transition: height 0.3s ease-in-out;`,
sandbox: "allow-scripts allow-same-origin",
},
});
iframe.addEventListener("load", () => {
const doc = iframe.contentWindow.document;
const plotlyDiv = doc.querySelector(".plotly-graph-div");
// Detect height from the Plotly object or fallback to 470
const heightValue = plotlyDiv ? parseInt(plotlyDiv.style.height) : 450;
iframe.style.height = `${heightValue + 20}px`;
iframe.style.visibility = "visible";
});
}
/**
* Displays an error message in the output area
* @param {string} message - error text
* @param {string} type - error type (e.g., "Error", "Warning")
* @param {HTMLElement} container - the output area in which to display the error
*/
showError(container, message, type = "Error") {
container.empty(); // Clear previous content
const errorWrap = container.createEl("div", { cls: "local-py-error-container" });
errorWrap.createEl("strong", { text: `${type}: ` });
errorWrap.createEl("code", { text: message });
}
/**
* Helper to construct the path to the python executable
* @returns {string} - the full path to the python executable in the venv
*/
getPythonPath() {
const vaultPath = this.app.vault.adapter.getBasePath();
// Use path.join to handle cross-platform slashes correctly
return path.join(vaultPath, "Scripts", ".venv", "bin", "python3");
}
};