Any examples in Python?

Hello Forio.

I am currently interested in comparing scenarios - something akin to your demonstration at https://www.youtube.com/watch?v=LIsjMGw0hUY

It works with Excel alright but I’ve been struggling to reproduce that example in Python for a while now.

In fact, I haven’t been able to make even the most simple example shown at https://forio.com/epicenter/docs/public/how_to/example_html/#model-code work with the Interface Builder.

Any chance you have a tutorial or a more complete example available covering Python?

To make it very specific, as a minimal example I’d like to have something like

initial_value = 1
growth = 2


def calculate_values():
    values = [initial_value * growth ** i for i in range(12)]
    return values  # returns [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048]

and be able to adjust initial_value and growth, and plot the values using the Interface Builder.

Do I necessarily need to Epicenter.record any of the values? Or make the variables global? Or create a context file?

Hi –

Global variables and functions in python can be accessed in HTML with FlowJs. Unfortunately the interface builder is not compatible with Python, so you have to make your own.

Here’s a simple example.

Create a model and upload it to the model folder, called “model.py”. Note that I’ve added a global variable with the calculated results to the model.

initial_value = 1
growth = 2

calculated_results = []

def calculate_values():
    global calculated_results
    calculated_results = [initial_value * growth ** i for i in range(12)]
    return calculated_results

Create a file “index.htm” in the interface builder, with this content

<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">
        <h1> Exponential Growth Simulation </h1>
        
        <h2>Inputs </h2>
        <div>
            Initial Value: <input data-f-bind="initial_value"> <br>
            Growth: <input data-f-bind="growth"><br>
            
            <button data-f-on-click="calculate_values()" value="Calculate">Calculate</button>
        </div>
        
        <h2>Outputs</h2>
        <div>
            Final Result: <span data-f-bind="calculated_results"></span> <br>
            All Results: <br>
            <div data-f-repeat="calculated_results"></div>
        </div>
    </body>
</html>

Then click Run Project to run the sim. You should see something similar to the following.

Click the Calculate button, and outputs will appear.

This very quickly constructed example shows how FlowJs works. The “data-f-bind” attribute automatically allows a variable to be set by an input tag, or displayed in a span tag. Data can also be looped (in this case showing a series of divs). Create a graph with the Forio Contour library following instructions here:
https://forio.com/epicenter/docs/public/data_binding_flow_js/graphing-overview/

Hope this helps you to get started!

WILL

Thank you Will for taking the time to provide an example.

Is there a particular reason why Python is not supported in the Interface Builder? Will it ever be?
What are the languages currently supported in the Interface Builder (out of the remaining Forio SimLang, Julia, Powersim, R, Stella, Vensim)?

Having the model exposed through JS libraries is highly flexible but requires frontend engineering expertise to build presentable dashboards whereas the Interface Builder is friendly to non-technical people.

Hi Pranas–

Thanks much for the note. You’ve described this accurately. Forio provides toolkits that work for authors at multiple levels. The interface builder is the simplest approach to building an interface, requiring no programming. FlowJs requires HTML development. There’s also lower-level APIs available for JavaScript and other programmers.

All of these tools are under development and evolve over time. At this point, the Interface Builder has templates that are focused on time based models. You can do a “run comparison”, a “turn by turn game” or a “multiplayer” turn by turn game. These templates works with Excel, Vensim, Powersim, STELLA, and SimLang.

We have developed an experimental “single page calculator” template that works with Python (and does not assume all variables are time based), but it needs some further development and documentation before I’d recommend it.

WILL