While trying to accomplish what I thought would be a quick and simple task - making a numerical model interactive while it’s running - I’ve ended up going down a bit of a rabbit hole. The latest side tunnel is RxPy, a library for “reactive programming” (which follows the design of an apparently widely used JS library called RxJS):
Being mindful of the First Law of Holes, before I dig any deeper I’d be curious to know whether others here have used this library, and/or have used other techniques to implement codes that have the ability to listen for and react to user inputs (for example, a user increasing or decreasing the value of a physical input parameter while a model is running).
I have used Julia’s Observables.jl through the Makie plotting library to do some reactive programming, mostly to build interactive data visualizations. It is a fairly well-established paradigm for building user interfaces, especially in functional programming languages which discourage traditional approaches to UI that maintain all of your interface’s state in a big mutable object.
I have never tried using it to update models while they are running, but that does sound like a cool way to experiment! I did once use Makie to make a UI for evaluating different parameter combinations for one of my models. It is a very simple model, so it is really fast, and it was possible to listen for changes in the parameter values from the UI, rerun the whole thing and visualize the output. I don’t think any of the reactive code made it into that tech report though…
That sounds like an interesting task! If it’s a command line application and the user input you want is simple, then could using input() be a straightforward way to go?
In terms of more elaborate interfaces, I have used Plotly’s Dash library a bit in the past. It’s mostly targetted at visualising data, but I don’t see why you couldn’t use it for your use case too. This is the relevant docs page on accepting user input. There’s also Shiny for Python. I’ve never used it but the R folks seem to love R Shiny, so I suspect it’s pretty good!
I’m curious what your use case is - are you getting the user to input data (and why does it have to be mid-run), or are you asking them to make a decision (based on results from what has already run?)?
I’m going to have to remember the First Law of Holes!!
I have some experience with Plotly Dash, Shiny Python and Streamlit. The three allow you to create a web app that has the concept of UI components that are connected with some Python code in the backend. In general, those libraries have connections for the most common Bootstrap components and common Python outputs as text, tables, and graphics. If you want to use more “sophisticated” outputs, such as maps or JavaScript libraries, you need to install some complementary libraries. My favorite right now in Python is Dash because it has a way to structure the code that is more clear for me than Streamlit. Shiny for R is my favorite because it has been developed since 2012, so there are a lot of basic and advanced use cases developed and as examples. Shiny for Python has some good examples too, like this one that shows how to create an app with reactive components and compiled machine learning code
Another option could be to create a Python program with a GUI interface with Tkinter
Thanks everyone! The idea is to allow a user to play with a landscape evolution model in real time, tweaking parameters as it runs. So for example, you could turn up or down the precipitation rate and watch the simulated landscape respond. I haven’t made much progress figuring out how to implement this, and haven’t had time to explore plotly dash, tkinter, but so far I’m thinking the most practical solution may be to use Jupyter widgets combined with multi-threading. (But sabbatical is now over so my recreational coding time is getting squeezed …
That sounds like an amazing project! It would be really cool to visualise that in the middle of a model. It would be so great for teaching; it’s often quite difficult to explain the effects of parameters to students when talking about models like this. Looking forward to hear more about this!