I don't know, where is my fault. DataTables is not showing any data. Is there an error with the code I have made? The flow is that I upload a csv file and after clicking the submit button it will redirect to the url /training which will display data with DataTables
Here is my flask code:
from flask import Flask, render_template, request, session, send_from_directory
from werkzeug.utils import secure_filename
from utils_process import text_preprocessing
app = Flask(__name__)
@app.route('/training', methods=['GET','POST'])
def training():
global data
if request.method == 'POST':
uploaded_df = request.files['file']
data_filename = secure_filename(uploaded_df.filename)
uploaded_df.save(os.path.join(app.config['UPLOAD_FOLDER'], data_filename))
session['uploaded_data_file_path'] = os.path.join(app.config['UPLOAD_FOLDER'], data_filename)
filepath = session['uploaded_data_file_path']
dict_data = []
with open(filepath, encoding="utf8") as file:
csv_file = csv.DictReader(file)
for row in csv_file:
dict_data.append(row)
data = []
for row in dict_data:
row['text_clean'] = text_preprocessing(row['content'])
data.append({
'content': row['content'],
'text_clean': row['text_clean'],
'rating': row['rating'],
'sistem': row['sistem'],
'layanan': row['layanan'],
'transaksi': row['transaksi'],
'pendaftaran_subsidi': row['pendaftaran subsidi'],
'kebermanfaatan': row['kebermanfaatan']
})
print(data)
return render_template('training.html', data=json.dumps(data))
if __name__ == '__main__':
app.run(debug=True)
Here is my template code :
{% extends "layout.html" %}
{% block content %}
<div class="container">
<div class="p-5 bg-white rounded shadow mb-5">
<table id='trainingTable' class='display dataTable' width='100%'>
<thead>
<tr>
<th>Content</th>
<th>Text Clean</th>
<th>Rating</th>
<th>Sistem</th>
<th>Layanan</th>
<th>Transaksi</th>
<th>Pendaftaran Subsidi</th>
<th>Kebermanfaatan</th>
</tr>
</thead>
</table>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
$('#trainingTable').DataTable({
'ajax': {
'url':'/training',
'dataType': 'json',
},
searching: true,
sort: true,
scrollY: 200,
scrollX: true,
'columns': [
{ data: 'content' },
{ data: 'text_clean' },
{ data: 'rating' },
{ data: 'sistem' },
{ data: 'layanan' },
{ data: 'transaksi' },
{ data: 'pendaftaran_subsidi' },
{ data: 'kebermanfaatan' },
]
});
});
</script>
{% endblock %}


dataSrc: ''to the'ajax': { ... }section of your DataTable definition. If this works, that is because the JSON is an unnamed array - so you can refer to that using an empty string''. SeedataSrc. If that does not work, show us the structure of your JSON (how it starts).