Mar-10-2020, 12:48 PM
Hi!
I have just started learning Flask. Turns out I need Ajax for my code to work and I canT figure it out. I have and index.html page. I have two buttons on it. The buttons call two different functions in the main app.py. I've got the ajax part from online, problem is, no matter which button I press after launching the localhost it only runs once. The function is called, it asks for a file via tkinter.filedialog, does some modifications and outputs an excel file to the users desktop. After that, no matter which button I press nothing happens( I stay on the same page if that matters). If I start pressing the buttons multiple times I get "raise full". this would be just a one page website, later I would like to add more buttons with more python functions. What am I doing wrong?
Here is part of the html which matters:
I have just started learning Flask. Turns out I need Ajax for my code to work and I canT figure it out. I have and index.html page. I have two buttons on it. The buttons call two different functions in the main app.py. I've got the ajax part from online, problem is, no matter which button I press after launching the localhost it only runs once. The function is called, it asks for a file via tkinter.filedialog, does some modifications and outputs an excel file to the users desktop. After that, no matter which button I press nothing happens( I stay on the same page if that matters). If I start pressing the buttons multiple times I get "raise full". this would be just a one page website, later I would like to add more buttons with more python functions. What am I doing wrong?
Here is part of the html which matters:
<!DOCTYPE html>
<html>
<head>
<title>test website</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
<link rel="shortcut icon" href="{{ url_for('static', filename='App.ico') }}">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
$(function() {
$('a#chase-button').bind('click', function() {
$.getJSON('/ConvertJpmChaseFunction',
function(data) {
//do nothing
});
return false;
});
});
</script>
<script type=text/javascript>
$(function() {
$('a#statestreet-button').bind('click', function() {
$.getJSON('/ConvertStateStreetFunction',
function(data) {
//do nothing
});
return false;
});
});
</script>
</head>
......
<tr>
<td>
<form>
<a href=# id=statestreet-button>
<button class="start-buttons" type="button">START</button>
</a>
</form>
</td>
<td>
<form>
<a href=# id=chase-button>
<button class="start-buttons" type="button">START</button>
</a>
</form>
</td>
</tr>And here is the main part of the app.py:from flask import Flask, render_template
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from io import StringIO
#from tkinter import *
import tkinter
#import mttkinter
from tkinter import filedialog
from tkinter import messagebox
import os, threading, re, time, shutil
import pandas
from pandas import read_excel
app = Flask(__name__)
@app.route("/")
def index():
return render_template('index.html')
################################# --------- FUNCTIONS --------- ##########################################
def convert(file, pages=None):
#function that runs in other functions
def isUndesignated(e):
#function that runs in other functions
@app.route('/ConvertJpmChaseFunction')
def ConvertJpmChaseFunction():
PdfReportFile = filedialog.askopenfilename(initialdir = "/",title = "Select PDF Report",filetypes = (("PDF File","*.pdf"),("All files","*.*")))
#long unrelated code
OutputDF.to_excel('C:\\users\\' + os.getlogin() + '\\Desktop\\' + PdfReportFile.split('/')[-1].replace("pdf", "xlsx"), index = None)
@app.route('/ConvertStateStreetFunction')
def ConvertStateStreetFunction():
PdfReportFile = filedialog.askopenfilename(initialdir = "/",title = "Select PDF Report",filetypes = (("PDF File","*.pdf"),("All files","*.*")))
#long unrelated code
OutputDF.to_excel('C:\\users\\' + os.getlogin() + '\\Desktop\\' + PdfReportFile.split('/')[-1].replace("pdf", "xlsx"), index = None)
if __name__ == "__main__":
app.run(debug=True)Why is it that i can't run the functions more than once?
