JetBrains / kotlin-playground

Self-contained component to embed in websites for running Kotlin code
https://jetbrains.github.io/kotlin-playground/examples/
Apache License 2.0
447 stars 80 forks source link

How to pass command line arguments dynamically #175

Open sheu opened 1 year ago

sheu commented 1 year ago

According to you documentation, one can pass arguments as follows

<code args="1 2 3">
/*
Your code here
*/
</code>

However, I would like to command line arguments dynamically. In my case I have an application where the users enter inputs and the code will be run against these inputs. So I would like to pass those inputs and command line arguments.

For the purpose of this question, the input will come from another textbox in on my page.

nikpachoo commented 1 year ago

Hi @sheu! The widget does not support changing command line arguments dynamically. In this case, you need to recreate the widget with new parameters. Something like that:

let instance;

initPlayground(element, theme) {
  if (instance) {
    instance.destroy();
  }

  playground(element, {
    getInstance: playgroundInstance => instance = playgroundInstance,
    args: '1 2 3'
  });
}