# Run python code from javascript Jupyterlab

**URL:** <https://discourse.jupyter.org/t/run-python-code-from-javascript-jupyterlab/24627>\
**Category:** JupyterLab\
**Tags:** jupyterlab, help-wanted, notebook\
**Created:** [March 19, 2024, 6:04am UTC](https://discourse.jupyter.org/t/run-python-code-from-javascript-jupyterlab/24627 "2024-03-19T06:04:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![immanoj16](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/immanoj16/32/12789_2.png) [@immanoj16](https://discourse.jupyter.org/u/immanoj16)\
**Post date:** [March 19, 2024, 6:04am UTC](https://discourse.jupyter.org/t/run-python-code-from-javascript-jupyterlab/24627/1 "2024-03-19T06:04:31Z")

</div>

Hi,

I am able to run python code from Javascript using Jupyterapp. But it is not working like I want.

1. Enabled jupyterapp `c.LabApp.expose_app_in_browser = True`
2. Running the following code to get cookies from the browser

```auto
from IPython.display import display, Javascript
js_code = """
async function run() {
    const session = jupyterapp.shell.currentWidget.context.sessionContext.session;
    const kernel = jupyterapp.serviceManager.sessions._connectToKernel({model: {id: session._kernel._id, name: session._kernel._name}})
    const cookies = document.cookie.split(';').reduce((acc, cookie) => {
        const [name, value] = cookie.trim().split('=');
        acc[name] = value;
        return acc;
    }, {});
    await kernel.requestExecute({code: `
    globals()["access"] = ` + `'${cookies.access}';
    globals()["refresh"] = ` + `'${cookies.refresh}';
    `}).done;
    console.log("executed")
}

run()
"""
display(Javascript(js_code))
print(globals()['access'])
print(globals()['refresh'])

```

Now I am not getting the global() variables access after display function. After sometime, if I print those variables in a new cell it is working. But in the same cell it throws KeyError even after time.sleep.

Is there any better way to do it?
