Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 14a9d1d

Browse files
committed
Working with unique python exes
1 parent 3b3b605 commit 14a9d1d

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ print(f"This is the open file: {abs_file_path}")
7676

7777
## Settings
7878

79-
You have the ability to change the python script location in settings. Additionally you have the ability to change which python executable is used.
79+
You have the ability to change the default python script location in settings. Additionally you have the ability to change the default python executable, or can configure python executables per script if you would like to use virtual environments. i.e. Python Exe: C:\Users\nickr\Documents\Projects\Scratch\.venv\Scripts\python.exe
8080

8181
## Debugging
8282

83-
If your script fails to run. An error is shown in the top right of obsidian and in the *developer console* which can be found with the following hotkeys: "ctrl" + "shift" + "i" on Windows, or "cmd" + "option" + "i" on MacOS
83+
If your script fails to run. An error is shown in the top right of obsidian and in the *developer console* which can be found with the following hotkeys: "ctrl" + "shift" + "i" on Windows, or "cmd" + "option" + "i" on MacOS.

main.ts

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import { exec } from 'child_process';
77
interface PythonScripterSettings {
88
pythonPath: string;
99
pythonExe: string;
10+
pythonIndividualExes: { [index: string]: any };
1011
// useLastFile: boolean;
1112
}
1213

1314
const DEFAULT_SETTINGS: PythonScripterSettings = {
1415
pythonPath: "",
1516
pythonExe: "",
17+
pythonIndividualExes: {},
1618
// useLastFile: false
1719
}
1820

@@ -67,23 +69,31 @@ export default class PythonScripterPlugin extends Plugin {
6769
console.error(err);
6870
return;
6971
}
70-
let python_exe = "python";
72+
let python_exe = "";
7173
if (this.settings.pythonExe != "") {
7274
python_exe = this.settings.pythonExe
7375
}
76+
else {
77+
python_exe = "python"
78+
}
79+
80+
if (fileName in this.settings.pythonIndividualExes) {
81+
python_exe = this.settings.pythonIndividualExes[fileName];
82+
if (!fs.existsSync(this.settings.pythonIndividualExes[fileName])) {
83+
new Notice(`Python Exe: ${this.settings.pythonIndividualExes[fileName]} for ${fileName} does not exist`)
84+
console.log(`Python Exe: ${this.settings.pythonIndividualExes[fileName]} for ${fileName} does not exist`)
85+
return;
86+
}
87+
88+
}
89+
console.log(`Python Exe: ${python_exe}`)
7490
if (stats.isFile()) {
7591
// var local_current_file_path = this.app.workspace.activeEditor?.file?.path;
7692
var local_current_file_path = this.app.workspace.getActiveFile()?.path?.toString();
7793
if (local_current_file_path === undefined) {
7894
// local_current_file_path = "";
7995
local_current_file_path = "";
8096
}
81-
// if (this.settings.useLastFile) {
82-
// local_current_file_path = this.app.workspace.getActiveFile()?.name?.toString();
83-
// }
84-
// else {
85-
// local_current_file_path = this.app.workspace.activeEditor?.file?.path;
86-
// }
8797

8898

8999

@@ -94,6 +104,7 @@ export default class PythonScripterPlugin extends Plugin {
94104
return;
95105
}
96106
new Notice(`Script ` + fileName + ` output:\n${stdout}`);
107+
console.log(`Script ` + fileName + ` output:\n${stdout}`)
97108
});
98109
} else if (stats.isDirectory()) {
99110
var dir = path.join(filePath);
@@ -108,6 +119,7 @@ export default class PythonScripterPlugin extends Plugin {
108119
return;
109120
}
110121
new Notice(`Script ` + fileName + " " + basePath + ` output:\n${stdout}`);
122+
console.log(`Script ` + fileName + " " + basePath + ` output:\n${stdout}`)
111123
});
112124
}
113125
});
@@ -118,7 +130,7 @@ export default class PythonScripterPlugin extends Plugin {
118130
}
119131

120132
// This adds a settings tab so the user can configure various aspects of the plugin
121-
this.addSettingTab(new PythonScripterSettingTab(this.app, this));
133+
this.addSettingTab(new PythonScripterSettingTab(this.app, this, files));
122134

123135
}
124136

@@ -137,10 +149,12 @@ export default class PythonScripterPlugin extends Plugin {
137149

138150
class PythonScripterSettingTab extends PluginSettingTab {
139151
plugin: PythonScripterPlugin;
152+
files: string[];
140153

141-
constructor(app: App, plugin: PythonScripterPlugin) {
154+
constructor(app: App, plugin: PythonScripterPlugin, files: string[]) {
142155
super(app, plugin);
143156
this.plugin = plugin;
157+
this.files = files
144158
}
145159

146160
display(): void {
@@ -159,7 +173,7 @@ class PythonScripterSettingTab extends PluginSettingTab {
159173
await this.plugin.saveSettings();
160174
}));
161175
new Setting(containerEl)
162-
.setName('Python Executable')
176+
.setName('Default Python Executable')
163177
.setDesc('Defaults to python')
164178
.addText(text => text
165179
.setPlaceholder('Enter path or command')
@@ -168,14 +182,21 @@ class PythonScripterSettingTab extends PluginSettingTab {
168182
this.plugin.settings.pythonExe = value;
169183
await this.plugin.saveSettings();
170184
}));
171-
// new Setting(containerEl)
172-
// .setName('Use Last File')
173-
// .setDesc('Run the script on the last file that was opened. This make it possible to run it on other file types e.g. pdf.')
174-
// .addToggle(toggle => toggle
175-
// .setValue(this.plugin.settings.useLastFile)
176-
// .onChange(async (value) => {
177-
// this.plugin.settings.useLastFile = value;
178-
// await this.plugin.saveSettings();
179-
// }));
185+
186+
for (var index = 0; index < this.files.length; index++) {
187+
let file = this.files[index];
188+
new Setting(containerEl)
189+
.setName(`${file} Python Executable`)
190+
.setDesc(`Overides the default python executable for ${file}`)
191+
.addTextArea((area) => {
192+
area
193+
.setValue(this.plugin.settings.pythonIndividualExes[file])
194+
.onChange(async (value) => {
195+
this.plugin.settings.pythonIndividualExes[file] = value;
196+
await this.plugin.saveSettings();
197+
});
198+
});
199+
}
180200
}
201+
181202
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "python-scripter",
33
"name": "Python Scripter",
4-
"version": "1.0.7",
4+
"version": "1.0.8",
55
"minAppVersion": "0.15.0",
66
"description": "Run Python scripts directly as Obsidian commands.",
77
"author": "Nick Allison",

0 commit comments

Comments
 (0)