0

I want to do data preprocessing for machine learning app in flask. the flow is, the html form send form to javascript code and then to the flask route.

this i my html form

<form method="POST" enctype ="multipart/form-data" id="train-form">
<input type="file" name="file_train" id="file_train">
<button type="submit" id="train_submit" class="btn btn-primary">Train Data</button>
</form>

this is my javascript code

$('#train-form').on('submit',function(){
var file_train = new FormData($('#train-form')[0]);
$.ajax({
    data : file_train,
    contentType: false,
    cache: false,
    processData: false,
    // async: false,
    type : 'post',
    url : '/training',
})
.done(function(data){
    $('#ulasan').html(data.ulasan);
    $('#case_folding').html(data.case_folding);
    $('#r_punctuation').html(data.r_punctuation);
    $('#r_stopwords').html(data.r_stopwords);
    $('#t_segmentation').html(data.t_segmentation);
    $('#stemming').html(data.stemming);
    $('#t_kata').html(data.t_kata);
    $('#preproccessing').show();
});
});

this is my flask route code

@app.route("/training", methods=('GET', 'POST'))
def training():
if request.method=="POST":
    file = request.files['file_train']

    df = model.read_data(file)
    df = model.data_preprocessing(df)
    # data_token = df['t_kata'] # tokenization

    ulasan = str(df['ulasan'][:5])
    case_folding = str(df['case_folding'][:5])
    r_punctuation = str(df['r_punctuation'][:5])
    r_stopwords = str(df['r_stopwords'][:5])
    t_segmentation = str(df['t_segmentation'][:5])
    stemming = str(df['stemming'][:5])
    t_kata = str(df['t_kata'][:5])
    return jsonify({'ulasan': ulasan, 'case_folding':case_folding, 'r_punctuation':r_punctuation, 'r_stopwords':r_stopwords, 't_segmentation':t_segmentation, 'stemming':stemming, 't_kata':t_kata})
else:
    return render_template('training.html', title='train')

at first, the page is rendered succesfully and when the preprocessing is finished, i have hidden table that will show up to show the data. but jsonify return blank json html page. the question is how i return jsonify to the my rendered page that is training.html ? thank you

this is the page that want to be rendered

this is the json raw html page

1 Answers1

0

The button type attribute should be type="button"

Mosallamy
  • 76
  • 6
  • thank you, i tried it but dont work. i think it doesnt matter, because in the javascript code, it accept 'submit' too. i think the problem is in the flask route. – M Ali Akbar Sinaga Oct 10 '22 at 03:04
  • Can you try to add onlick event listener to train_submit button I checked the code and it worked fine = – Mosallamy Oct 10 '22 at 08:05