Вот мой HTML. Есть 3 кнопки, которые добавляются в массив, и последняя кнопка, которая отправляет массив на python через. AJAX:
{% extends 'base.html' %}
{% block head %}
<title>PPO Count</title>
{% endblock %}
{% block body %}
<!-- Header -->
<div class="header">
<h2 class='title'>Prototype 1: Pet Owner Viz</h2>
</div>
<!-- Button Panell-->
<!-- Vis Box -->
<div class='canvas'>
<div class="container">
<div class='button-box'>
<button id='Bx' type="button" onclick='toggleClickedBuz("Bx", "Bx")'>Bx</button>
<button id='By' type="button" onclick='toggleClickedBuz("By", "By")'>By</button>
<button id='Bz' type="button" onclick='toggleClickedBuz("Bz", "Bz")'>Bz</button>
<button id='loadData'>Load Data</button>
</div>
<div class='viz-box'>
</div>
</div>
</div>
<!-- Scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="/static/js/main.js"></script>
<script type="text/javascript">
function toggleClickedBuz( bizStr , id ) {
if(clickedBusinesses.includes(bizStr)){
// removing duplicate element from that array, dependant on button pressed
clickedBusinesses = clickedBusinesses.filter( cb => cb !== bizStr );
document.getElementById( id ).style.backgroundColor='white';
}else{
// else push it to the array
clickedBusinesses.push(bizStr)
document.getElementById( id ).style.backgroundColor='red';
}
var json_string = JSON.stringify(clickedBusinesses)
console.log(json_string)
};
window.onload = function() {
document.getElementById('loadData').onclick = function() {
doWork()
};
}
function doWork(json_string) {
$.post('receiver', json_string, function() {
});
event.preventDefault();
}
</script>
{% endblock %}
Как вы можете видеть, массив должен быть загружен. Но я получаю TypeError: объект 'NoneType' не повторяется в python.
import sys
from flask import Flask, render_template, request, redirect, Response
import random, json
app = Flask(__name__)
@app.route('/', methods = ['GET','POST'])
def index():
return render_template('index.html')
@app.route('/receiver', methods = ['POST'])
def worker():
data = request.get_json()
result = ''
for item in data:
print(item)
return result
if __name__ == '__main__':
app.run(debug=True)
Можете ли вы помочь мне отправить мой массив JS на Python, используя AJAX, чтобы я мог запустить его через фрейм данных pandas, пожалуйста?