Home > Back-end >  Insert values from JavaScript to Python to file.xls
Insert values from JavaScript to Python to file.xls

Time:07-18

I'm trying to insert certain values into a xls or xlsx file, that have been fetched in JavaScript to the python function. How can I insert the values from the JavaScript function into the python function?

This is my python code:

import xlwt
from xlwt import Workbook

    @app.route('/Import_to_excel',
    methods=['GET', 'POST'])
    def Import_to_excel():
    
        x = request.args.get('id0')
        y = request.args.get('id1')
        z = request.args.get('id2')
    
        array = [ ip, app, ver]
        
        wb = Workbook()
        sheet1 = wb.add_sheet('Sheet 1')
    
        sheet1.write(0, 0, array[0])
        sheet1.write(0, 1, array[1])
        sheet1.write(0, 2, array[2])

        wb.save('xlwt example.xls')
        
        return "success"

I have also been trying to post a list with the values, instead of individually posting the values one by one. But i have some problems with request.args.getlist() on the Python side of things.

This is my JavaScript function

function Import_to_excel()
{
    id0 = document.getElementById("x").text
    id1 = document.getElementById("y").value
    id2 = document.getElementById("z").value

    const input_values = [ id0, id1, id2];

 fetch('http://127.0.0.1:5000/Import_to_excel?id0=' id0
 ).then(resp => resp.json())

 fetch('http://127.0.0.1:5000/Import_to_excel?id1=' id1
 ).then(resp => resp.json())

 fetch('http://127.0.0.1:5000/Import_to_excel?id2=' id2
 ).then(resp => resp.json())
}

The output in my xls file is blank.

CodePudding user response:

Python code

id = request.args.get('ID')
x = id.split('_*_')[0]
y = id.split('_*_')[1]
z = id.split('_*_')[2]

JavaScript

  var x = document.getElementById("x").value;
  var y = document.getElementById("y").value;
  var z = document.getElementById("z").value;
  var a = 'http://127.0.0.1:5000/initiate_excel_corvert?ID='x '_*_' y '_*_' z '_*_'
           fetch(a)
          .then(res => res.json())
          .then(res => console.log(res));
        }
  • Related