Skip to content

Commit

Permalink
Use stanio for data.py
Browse files Browse the repository at this point in the history
  • Loading branch information
magland committed Jun 27, 2024
1 parent 1c4e00e commit 1caa041
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const AnalysisPyFileEditor: FunctionComponent<Props> = ({fileName, fileContent,

// here's where we can pass in globals
const globals = pyodide.toPy({ _sp_example_global: 5 });
let script = fileContent
const script = fileContent
pyodide.runPython(script, {globals})
setStatus('completed')
}
Expand Down
28 changes: 5 additions & 23 deletions gui/src/app/prototypes/DataPyPrototype/DataPyFileEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const loadPyodideInstance = async () => {
stderr: (x: string) => console.error(x)
})
pyodide = p
await pyodide.loadPackage(['numpy'])
await pyodide.loadPackage(['numpy', 'micropip'])
const micropip = pyodide.pyimport('micropip')
await micropip.install('stanio')
return pyodide
} else {
return pyodide
Expand Down Expand Up @@ -53,29 +55,9 @@ const DataPyFileEditor: FunctionComponent<Props> = ({fileName, fileContent, onSa

// We serialize the data object to json string in the python script
script += '\n'
script += 'import numpy as np\n'
script += 'def _sp_serialize(x):\n'
script += ' if isinstance(x, dict):\n'
script += ' ret = {}\n'
script += ' for key in x:\n'
script += ' ret[key] = _sp_serialize(x[key])\n'
script += ' return ret\n'
script += ' elif isinstance(x, list):\n'
script += ' ret = []\n'
script += ' for val in x:\n'
script += ' ret.append(_sp_serialize(val))\n'
script += ' return ret\n'
script += ' elif isinstance(x, tuple):\n'
script += ' ret = []\n'
script += ' for val in x:\n'
script += ' ret.append(_sp_serialize(val))\n'
script += ' return ret\n'
script += ' elif isinstance(x, np.ndarray):\n'
script += ' return _sp_serialize(x.tolist())\n'
script += ' else:\n'
script += ' return x\n'
script += 'import stanio\n'
script += 'import json\n'
script += 'data = json.dumps(_sp_serialize(data))\n'
script += 'data = stanio.dump_stan_json(data)\n'
pyodide.runPython(script, {globals})

if (setData) {
Expand Down

0 comments on commit 1caa041

Please sign in to comment.