Answer the question
In order to leave comments, you need to log in
How to get output from js console to python code?
I have an html page with a button, when pressed, the js function is triggered
in this function, I can do what is displayed, I need to save it in txt and in a python variable,
the python code (?)console.log(document.querySelector("#output"));
@app.route('/')
def index():
mystr = [получить вывод из js]
return mystr
Answer the question
In order to leave comments, you need to log in
after/instead of outputting to the console, send your data to the server as a POST request with content in JSON format
var xhr = new XMLHttpRequest();
var json = JSON.stringify(document.querySelector("#output")); //здесь вставляем и отправляем Ваши данные
xhr.open("POST", '/', true)
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
// Отсылаем объект в формате JSON и с Content-Type application/json
xhr.send(json);
@app.route('/')
def index():
if request.method == 'POST':
data = request.json #получаете свои данные в формате строки и обрабатываете
To do this, you need to understand that js is executed in a completely different environment - in a browser that knows nothing about python (and python knows nothing about the browser). The only way to communicate between these environments is a network protocol, an ajax request over http is perfect for your task.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question