Esempio n. 1
0
def run(script: api.Script, user_config: SDict) -> SDict:
    """Run a datapane python script/module"""
    api._reset_runtime(params=user_config)

    # use the script id for unique, isolated dir per script
    env_dir = Path(script.id)
    setup_script(script, env_dir)

    with script_env(env_dir):
        # script_name = str(script.script) ## <module> ?
        try:
            # run script in unpacked dir wrapped around pre/post-commands
            run_commands(script.pre_commands)
            res_scope = exec_mod(script.script)
            run_commands(script.post_commands)
            return res_scope
        except SyntaxError:
            raise CodeSyntaxError.from_exception()
        except Exception:
            raise CodeRaisedError.from_exception(partial(filter_frame_by_filename, "<module>"))
Esempio n. 2
0
def test_params_loading(datadir: Path):
    """Test that the API allows loading params from the datapane file"""
    config_fn = datadir / "datapane.yaml"
    initial_vals = dict(p1="a", p3=3)

    assert len(dp.Params) == 0

    # load some values
    api._reset_runtime(initial_vals)
    assert len(dp.Params) == 2
    assert dp.Params["p1"] == initial_vals["p1"]

    # clear and load again
    api._reset_runtime({})
    assert len(dp.Params) == 0
    api._reset_runtime(initial_vals)

    # load from file
    dp.Params.load_defaults(config_fn=config_fn)
    # ensure values are merged
    assert len(dp.Params) == 3
    assert dp.Params["p1"] == "hello"
    assert dp.Params["p2"] == 4
    assert dp.Params["p3"] == initial_vals["p3"]