A
A
Asya2020-05-29 11:28:48
Python
Asya, 2020-05-29 11:28:48

How to pass a string from Python to JS?

I have this python code:

@app.route('/mypage', methods=['GET', 'POST'])
def mypage():
    myfile = 'somefile.txt'

    return render_template('pages/mypage.html', myfile=myfile)


and I need to pass this name 'somefile.txt' to the js script on the mypage.html page

when I do this:

let someFile = document.getElementById("txt_id").textContent;
                            someFile = JSON.stringify(someMol).replace(/ /g,"");
                            console.log(someFile); #тут выводится: "somefile.txt"

                document.addEventListener( "DOMContentLoaded", function(){
                stage.loadFile('../../path_to_my_file/' + someFile ).then( function( o ) {

                бла бла бла

                });
            }
            );


then crashes
mistake
Uncaught TypeError: NGL.loaderMap[c.ext] is not a constructor
and nothing is displayed,

but when I directly set it like this:

let someFile = document.getElementById("txt_id").textContent;
                            someFile = JSON.stringify(someMol).replace(/ /g,"");
                            console.log(someFile); #тут выводится: "somefile.txt"
                document.addEventListener( "DOMContentLoaded", function(){
                stage.loadFile('../../path_to_my_file/somefile.txt').then( function( o ) {

                бла бла бла

                });
            }
            );


then everything works

, I don’t understand how my string that I pass to loadFile differs from if I directly write the whole path?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question