Runing a python script with templater

Guys how do i run a script from python and save the output to put it inside obsidian?

i already configured as shell binary location my container folder of python scrpts i tried put it as in shell binary location hola and python3 file.py besides ping doesnt work or echo



Maybe you could write the output to a file and use tp.file.include to import it.

For example, if your Python script wrote the following to a file called ~temporary.md:

<%* const PYTHON_OUTPUT = "Hello, World!"; %>

Then, in your template, you could do:

<% tp.file.include("[[~temporary]]") %>

Here's your output: <% PYTHON_OUTPUT %>

I’m imagining this in my head, so it might not work! But, it’s what I would experiment with, if I were you.

Good luck! I’ll be curious to hear what you end up doing.

You might have some issue with paths, and file location. Are you sure python is available in your path, at the folder location the execution script is running in?

I’d try verifying those through some debug statements like cwd, which python3, or similar stuff related to your operating system.

meanwhile i was writing you a reply to your comment i encouter the solution.

your solution arise the same problem i would need to run the program manually and after use templater to replace it what i want it’s that templater run the python script and take the output inside obsidian yesterday i come up with the idea, if i use a javascript to do that run a command in terminal using chatgpt and a little google i came up with the following program

const { execSync } = require('child_process');

function ejecutarComando(comando) {

try {

const output = execSync(comando, { encoding: 'utf-8', shell: true });

return output.trim(); // Elimina cualquier espacio en blanco adicional alrededor del resultado

} catch (error) {

// Manejo de errores si el comando falla

console.error('Error al ejecutar el comando:', error.message);

return ''; // Devuelve una cadena vacĂ­a en caso de error

}

}

module.exports = ejecutarComando;

im scared to move anything inside this code i dont know what does each part

this program run any command in terminal as long as we pass it as a string for example

<%tp.user.command(“pwd”)%>

but if you run the command pwd to see where you are executing commands in my case in Ubuntu its Home dir

so if i move for example hello.py to $HOME dir i can run the command

<%tp.user.command(“python3 hello.py”)%>

it will give me the output from

print("Hello World!")

the reason to do this was to make a time id for zettelkasten with this program

<%tp.user.command(“python3 time_base36.py”)%>

import calendar
import datetime
date = datetime.datetime.utcnow()
utc_time = calendar.timegm(date.utctimetuple())
conversion_table = {0: '0', 1: '1', 2: '2', 3: '3',
4: '4', 5: '5', 6: '6', 7: '7',
8: '8', 9: '9', 10: 'A', 11: 'B',
12: 'C', 13: 'D', 14: 'E', 15: 'F',
16:'G', 17:'H', 18:'I', 19:'J', 20:'K',
21:'L', 22:'M', 23:'N', 24:'O', 25:'P',
26:'Q', 27:'R', 28:'S', 29:'T', 30:'U',
31:'V', 32:'W', 33:'X', 34:'Y', 35:'Z'}
def decimalToHexadecimal(decimal):
if(decimal <= 0):
	return ''
	remainder = decimal % 36
	return decimalToHexadecimal(decimal//36) + conversion_table[remainder]
print(decimalToHexadecimal(utc_time))

the expected output will be something like RX05WR this is the unix time in base 36 whose use all English alphabetical and numbers in this form its a more reduced id compared with the unique id 29062023010456

now if you want a specific folder where are store your python scripts at least for me i want no matter what i want they are the in the same folder so i store it in my vault so if you want execute your scripts need specify the folder

you need replace it with your path where you are gonna store your Scripts

const = directorio ='/home/path/vault'

const { execSync } = require('child_process');

function ejecutarComando(comando) {

try {

const directorio = '/home/user/Documents/Obsidian/user/Attachments/Public/Scripts/';

const output = execSync(`cd ${directorio} && `+comando, { encoding: 'utf-8', shell: true });

return output.trim(); // Elimina cualquier espacio en blanco adicional alrededor del resultado

} catch (error) {

// Manejo de errores si el comando falla

console.error('Error al ejecutar el comando:', error.message);

return ''; // Devuelve una cadena vacĂ­a en caso de error

}

}

module.exports = ejecutarComando;

If you need it for windows just ask it to chatgpt i use it to do this javascript i dont know anything about it

i almost sure there is a way to do it universal with powershell

as you read it my main language is Spanish so sorry for grammar mistakes

i dont know my os is ubuntu and if i do a simple user function as an echo it doesnt run at all it is marked as an error but i found the solution :grin:

remember
the javascript i save it as command.js and i specify the folder in templater settings

this is the same folder where i have store my python scripts

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.