thunderbug1 / streamlit-javascript

Streamlit component to execute javascript code on the client side and get back the result
MIT License
102 stars 3 forks source link

JavaScript code must start on first line of string #12

Open BenSchZA opened 1 year ago

BenSchZA commented 1 year ago

For anyone else who may run into this error (haven't dug into why this happens) the JavaScript code to execute must start on the first line of a multi-line string.

Returns 0:

    script = """
    await (async function () {
        var pairs = document.cookie.split(";");
        var cookies = {};
        for (var i = 0; i < pairs.length; i++) {
            var pair = pairs[i].split("=");
            cookies[(pair[0]+'').trim()] = unescape(pair.slice(1).join('='));
        }
        return cookies;
    })()
    """

Returns 10:

    script = """10
    await (async function () {
        var pairs = document.cookie.split(";");
        var cookies = {};
        for (var i = 0; i < pairs.length; i++) {
            var pair = pairs[i].split("=");
            cookies[(pair[0]+'').trim()] = unescape(pair.slice(1).join('='));
        }
        return cookies;
    })()
    """

Returns the expected value of cookies:

    script = """await (async function () {
        var pairs = document.cookie.split(";");
        var cookies = {};
        for (var i = 0; i < pairs.length; i++) {
            var pair = pairs[i].split("=");
            cookies[(pair[0]+'').trim()] = unescape(pair.slice(1).join('='));
        }
        return cookies;
    })()
    """
Socvest commented 1 year ago

Genius brother, genius. Thanks!

ghz commented 10 months ago

Not working for me. Even a simple script = 'await (async function () { return "test"; })()' return 0

BenSchZA commented 10 months ago

@GHz this is probably related to issue #14 .