Python Forum
Running Ajax multiple times on button click?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running Ajax multiple times on button click?
#1
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:

<!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?
Reply
#2
Nobody? :(
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Running an action only between certain times alexbca 9 1,658 Mar-15-2023, 04:21 PM
Last Post: deanhystad
  Code running many times nad not just one? korenron 4 1,324 Jul-24-2022, 08:12 AM
Last Post: korenron
Sad Iterate randint() multiple times when calling a function Jake123 2 1,979 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Running script on multiple files Afrodizzyjack 1 2,466 May-14-2021, 10:49 PM
Last Post: Yoriz
  rtmidi problem after running the code x times philipbergwerf 1 2,397 Apr-04-2021, 07:07 PM
Last Post: philipbergwerf
  How many times was the button pressed in pyglet rama27 0 1,888 Oct-10-2020, 10:26 AM
Last Post: rama27
  How to print string multiple times on new line ace19887 7 5,570 Sep-30-2020, 02:53 PM
Last Post: buran
  Copy mp3 file multiple times and rename Mar10 4 3,674 Sep-23-2020, 01:09 AM
Last Post: Mar10
  Scripting: Multiple Votes With a Single Click or Button Ovidiu 0 1,364 Jul-07-2020, 10:51 AM
Last Post: Ovidiu
  How to print string multiple times separated by delimiter Mekala 1 1,865 May-23-2020, 09:21 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020