Execute Dynamic python code

I would like to build a button template, which accepts as a custom parameter a string defining a
project script to run. I tried using "exec()" but it doesn't seem to work (see test in Script console screen shot below), no print output...

i.e. In the project library define some functions. In the button template call the function passed to it in the custom parameter string...

Can't use runScript() since it's an expression function.

Never mind...figured it out...had to put the "()" in the script call

Why? This is literally what it does?

Also, you haven’t said where this “button” is going to reside, but note that the Script Console is not the same as the gateway.

This is an astonishingly bad idea. Consider allowing a function name that must already exist in the project, and then you don't need exec(). Instead, use python's getattr to look up the function name in the project library, which will give you a function object, which you can then run directly.

3 Likes

The button will be in a template screen. The button will have other scripting so can't just call an expression and use runScript().

I will try using the getattr instead of the exec() call...
Thanks.

The use of exec and eval are considered to be a bad practice due to injection risks. Even if it seems safe for use in your smoker oven project, I still recommend avoiding unsafe practices for the sake of developing good habits.

3 Likes

Agree.

I would put this script in a library, and make a single call from the button. I see absolutely no reason for this to be "dynamic".