question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling a javascript function from python code?

See original GitHub issue

I’m trying to embed the Ace Editor in my application. https://ace.c9.io/#nav=embedding

with the following script in head_html, I just need to call the create_editor and destroy_editor functions with the appropriate strings.

Is there a way to invoke a JS function from the python backend?

<script src="/static/ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
    window.all_editors = {};
    function create_editor(element_id) {
        var editor = ace.edit(element_id);
        editor.setTheme("ace/theme/monokai");
        editor.session.setMode("ace/mode/markdown");
        window.all_editors[element_id] = editor;
    }
    function destroy_editor(element_id) {
        window.all_editors[element_id].destroy();
        delete window.all_editors[element_id];
    }
</script>

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10

github_iconTop GitHub Comments

2reactions
elimintzcommented, Feb 29, 2020

So this enhancement was very simple to implementand I added it to version 0.0.8 (coming out Monday).

I added a method to the WebPage class called run_javascript. It accepts as an argument a string with JS code.

This is the test example:

import justpy as jp

async def my_click(self, msg):
    await msg.page.run_javascript("""
    for (var i = 0; i<10; i++) {
        console.log('Line: ' + i);
        }
    """)

def javascript_test():
    wp = jp.WebPage()
    d = jp.Div(a=wp, classes='m-2')
    b = jp.Button(text='Run Script', a=d, classes=jp.Styles.button_simple, click=my_click)
    return wp

jp.justpy(javascript_test)

1reaction
elimintzcommented, Mar 4, 2020

If you are building a custom element, then you can use the Vue life cycle hooks such as mounted.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I call a Javascript function from Python? - Stack Overflow
To interact with JavaScript from Python I use webkit, which is the browser renderer behind Chrome and Safari. There are ...
Read more >
How to Run Javascript from Python ? - GeeksforGeeks
The js2py module provides one way for converting JS code to Python code we must use the translate_file() function for this. After the ......
Read more >
Run Javascript from Python| | Js2Py - Medium
In this Blog article, we will learn how to Run Javascript commands from inside Python script. We will make use of the Js2Py...
Read more >
How to Run JavaScript in Python - MakeUseOf
Declare a function using JavaScript format and enclose it in the multiline string (""" or '''). You can store it in a variable...
Read more >
How To Run Javascript From Python - Detailed Guide?
To run a JavaScript function from python, create a function and assign it into a variable. Then invoke that function using the eval_js()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found