# Executing python expression from module

**URL:** <https://forum.inductiveautomation.com/t/executing-python-expression-from-module/15544>\
**Category:** Module Development\
**Tags:** ignition79, scripting\
**Created:** [October 3, 2017, 11:38am UTC](https://forum.inductiveautomation.com/t/executing-python-expression-from-module/15544 "2017-10-03T11:38:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![a.bolatov](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@a.bolatov](https://forum.inductiveautomation.com/u/a.bolatov)\
**Post date:** [October 3, 2017, 11:38am UTC](https://forum.inductiveautomation.com/t/executing-python-expression-from-module/15544/1 "2017-10-03T11:38:28Z")

</div>

I’d like to execute a python expression and get the result as a String from inside my module. There is a swing component which has dynamic title. Let’s say, I pass python expression ‘now(1000)’, without quotes, through property editor and the title will be dynamically showing datetime string.

According to the answer from @Carl.Gould at [this thread](http://forum.inductiveautomation.com/t/designer-out-of-memory-error/6493/5) I have created a `ScriptRunner` class.

```auto
public class ScriptRunner {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private final ScriptManager scriptManager;

    public ScriptRunner(ScriptManager scriptManager) {
        assert scriptManager != null : "ScriptManager cannot be null";
        this.scriptManager = scriptManager;
    }

    public String runIt(String scriptCode) {

        if (StringUtils.isBlank(scriptCode)) {
            logger.debug("runIt(ScriptCode) is null or empty");
            return "";
        }

        PyObject compiledScript =
                Py.compile_command_flags(scriptCode,
                        "<whatever-filename>",
                        CompileMode.eval,
                        CompilerFlags.getCompilerFlags(),
                        false);
        try {
            return scriptManager.runFunction(compiledScript).asString();
        } catch (JythonExecException | NullPointerException e) {
            logger.error("Error running {}", scriptCode, e);
        }
        return "NO_RESULT";
    }

}

```

I get an instance of the `ScriptManager` from the component by calling:

```auto
VisionClientContext context = getAppContext();
ScriptManager scriptManager = context.getScriptManager();
this.scriptRunner = new ScriptRunner(scriptManager);

```

Unfortunately, when I call `scriptRunner.runIt("now(1000)")` I get an exception:

```auto
com.inductiveautomation.ignition.common.script.JythonExecException: TypeError: 'tablecode' object is not callable
	at org.python.core.Py.TypeError(Py.java:235)
	at org.python.core.PyObject. __call__ (PyObject.java:316)
	at org.python.core.PyObject. __call__ (PyObject.java:357)
	at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:670)
	at mycompany.ScriptRunner.runIt(ScriptRunner.java:35)

```

Could help figuring this out?

Thanks in advance,  
Almer

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [October 3, 2017, 11:46am UTC](https://forum.inductiveautomation.com/t/executing-python-expression-from-module/15544/2 "2017-10-03T11:46:05Z")

</div>

`now()` is part of our expression language, which is an entirely separate thing from executing arbitrary Jython code.

---

<div class="post-metadata">

**Author:** ![a.bolatov](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@a.bolatov](https://forum.inductiveautomation.com/u/a.bolatov)\
**Post date:** [October 3, 2017, 11:48am UTC](https://forum.inductiveautomation.com/t/executing-python-expression-from-module/15544/3 "2017-10-03T11:48:16Z")

</div>

Hi @Kevin.Herron ,

I am new to Ignition platform. I might have mis-explained my problem. What if I only want to execute Ignition expressions and get back the results as string, for example?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [October 3, 2017, 11:54am UTC](https://forum.inductiveautomation.com/t/executing-python-expression-from-module/15544/4 "2017-10-03T11:54:35Z")

</div>

[This post](http://forum.inductiveautomation.com/t/alarmnotification-how-to-parse-expressions-in-a-property/7655) may be helpful. You can ignore the alarm-specific parts, but basically what you need is an `ExpressionParseContext`. Searching for that term yields a couple other results too.

---

<div class="post-metadata">

**Author:** ![a.bolatov](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@a.bolatov](https://forum.inductiveautomation.com/u/a.bolatov)\
**Post date:** [October 4, 2017, 9:48am UTC](https://forum.inductiveautomation.com/t/executing-python-expression-from-module/15544/5 "2017-10-04T09:48:39Z")

</div>

Hi @Kevin.Herron,

thanks, I got it working using [this class you wrote](http://forum.inductiveautomation.com/t/expression-parsing/9170/7) and [this usage example](http://forum.inductiveautomation.com/t/expression-parsing/9170/5).

In case if anyone needs a running example, I will leave the following code snippets here:

```auto
public class CustomParseContext implements ExpressionParseContext {
    private final TagManagerBase tagManagerBase;
    private final FunctionFactory functionFactory;

    public CustomParseContext(TagManagerBase tagManagerBase, FunctionFactory functionFactory) {
        this.tagManagerBase = tagManagerBase;
        this.functionFactory = functionFactory;
    }

    @Override
    public Expression createBoundExpression(String path) throws RuntimeException {
        try {
            TagPath tagPath = TagPathParser.parse(path);
            Tag tag = tagManagerBase.getTag(tagPath);

            if (tag == null) throw new TagNotFoundException(tagPath);

            TagValue tagValue = tag.getAttribute(tagPath.getProperty());

            TagListener listener = new TagListener(
                    tagPath,
                    tagValue.getValue(),
                    tagValue.getQuality()
            );

            Class type = TagProp.Value.equals(tagPath.getProperty()) ?
                    tag.getDataType().getJavaType() :
                    tagPath.getProperty().getType();

            BoundTagExpression expression = new BoundTagExpression(tagPath);
            expression.setType(type);
            expression.setTagListenerDelegate(listener);

            return expression;
        } catch (IllegalArgumentException | IOException | TagNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public FunctionFactory getFunctionFactory() {
        return functionFactory;
    }
}

```

```auto
public class MyComponent extends AbstractVisionPanel {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    ...

    @Override
    protected void onStartup() {
        super.onStartup();

        VisionClientContext appContext = getAppContext();
        FunctionFactory functionFactory = appContext.getExpressionFunctionFactory();
        ClientTagManager tagManager = appContext.getTagManager();

        CustomParseContext customParseContext = new CustomParseContext(tagManager, functionFactory);
        Parser parser = new ELParserHarness();
        String expressionString = "now(1000)";
        try {
            Expression expression = parser.parse(expressionString, customParseContext);
            expression.connect(appContext, () -> {
                try {
                    logger.debug("updating");
                    String value = TypeUtilities.toString(expression.execute().getValue());
                    System.out.printf("value = %s\n", value);
                } catch (ExpressionException e) {
                    logger.warn("Error parsing expression '{}'", expressionString);
                }
            });

            expression.startup();

            // Execute the expression 'now(1000)' once
            System.out.println("execute: " + TypeUtilities.toString(expression.execute().getValue()));
        } catch (Exception e) {
            logger.error("Error starting ELParser", e);
        }
    }
    ...
}

```

**I will mark this question as solved if you don’t mind**
