Hello everyone,
I am trying to consume the Forio Epicenter API in Python, however, I have not succeeded, does anyone know how to make the connection, could you help me.
Thank you very much.
Hello everyone,
I am trying to consume the Forio Epicenter API in Python, however, I have not succeeded, does anyone know how to make the connection, could you help me.
Thank you very much.
Hi,
To make effective use of Python models you must create your user interfaces in HTML-- we recommend using the Flow JS library. This let’s you bind HTML tags to model variables, and buttons to call model functions.
For example, here’s a trivial python model with a global variable and a function:
def double():
global abc
abc = abc*2
return abc
Save this as a file model.py in the model folder.
Then create a file “index.html” in the “interface” folder
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>
<script src="//forio.com/tools/js-libs/2.9.1/epicenter.min.js"></script>
<script src="//forio.com/tools/js-libs/flow/latest/flow.js"></script>
<script>
$(function() {
Flow.initialize();
});
</script>
</head>
<body data-f-model="model.py">
<p> The value of the variable 'abc' is: <span data-f-bind="abc"></span></p>.
<p>Call the model function 'double' by clicking the button below.</p>
<p><button data-f-on-click="double" />Double the Value</p>
</body>
</html>
When you run the sim, it looks like this:
Clicking the button calls the ‘double’ function
That’s the basics.
Some details on working with Python in Epicenter are here
https://forio.com/epicenter/docs/public/model_code/python/
FlowJs docs are here
https://forio.com/epicenter/docs/public/data_binding_flow_js/
Javascript APIs are also available for more sophisticated simulation behavior:
https://forio.com/epicenter/docs/public/api_adapters/
Good luck! Let us know if you have further questions!
did anything change? I tried the above but it does not work
Stefano - thanks for reaching out about this. Can you please provide the URL to the project you are working on so we can review?
Best,
Rob Pratt @ Forio
Thanks, Stefano!
We updated your model to include the following line at the top:
abc = 1
Before this, the project couldn’t find the variable abc
because it wasn’t defined at the top level of the model. You should now be able to successfully run the project.
If you run into additional issues, you can take a look at the browser console for relevant errors. In this case, a 404 was returned to the GET call for .../variables/?include=abc
with a message that the variable could not be found.
Also, please reach out to us with any additional questions you have.
Rob Pratt
ok understood, but I replaced the abc
with mytest
in both the html and python and now it’s broken, the new mytest
variable is not found, do I need to deploy the app in a special way to make sure the new variable gets understood?
p.s. ah ok I didn’t know about the reset button, I assume that triggers something as I don’t have any reset on python.
One last thing, can you check also what’s wrong now, I just put a tag input rather than a span, but the variables are not found anymore (even after I reset)
Hi Stefano. Your latest model has a typo “result” instead of “results”.
Try running the model in a Python IDE like PyCharm or Visual Studio Code first to test it.
WILL
Also, please note you must reset the model each time it changes to start a new run that uses the model. Add a reset button to the web page. (Same format as the other button, but use operation “reset”).