A
A
Asya2020-05-18 16:54:39
Python
Asya, 2020-05-18 16:54:39

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

2 answer(s)
M
myxomor469, 2020-05-18
@asyaevloeva

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);

on the server side, get the data from the request and process it as you like
@app.route('/')
def index():
    if request.method == 'POST':
        data = request.json  #получаете свои данные в формате строки и обрабатываете

A
abberati, 2020-05-18
@abberati

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 question

Ask a Question

731 491 924 answers to any question