-2

i'm bulding a little app that uses a small UI to take info from the user, and after all the info is entered, the user can generate a PDF with every entry stored, like this:

UI for the user to enter data

Expectec output in PDF

As you can see in the image #2 to generate the PDF, i'm using a HTML template with jinja to pass the values with the following function:

    **THIS IS A CLASS METHOD THAT RETURNS THE DATA TO GENERATE THE PDF**
    def get_datos(self):

        """
        Genera un diccionario con cada banco y la cantidad de depositos por banco
        para poder generar los PDF de los depositos
        """
        
        bancos = {}
        montos_a_depositar = [a[2] for a in (r for r in self.get_listado_de_depositos())]
        envasadoras = [str(a[3]).split(";")[0] for a in (r for r in self.get_listado_de_depositos())]
        for i in  (p for p in self.get_listado_de_depositos()):
            bancos [i[1]] = bancos.get(i[1],0)+1
        
        return (bancos,montos_a_depositar,envasadoras)



app = Flask(__name__)

@app.route("/")
def generador_depositos(banco):

        
        fecha_deposito = datetime.today().strftime("%B %d, %Y")
        #envasadora = ventana.self.get_listado_de_depositos()[0][3]
        #monto = ventana.get_listado_de_depositos()[0][2]
        return render_template(banco,fecha_deposito=fecha_deposito)

if __name__ == "__main__":

    app_window = Tk()
    app_window.geometry ("255x120")
    a_generar = ventana(app_window)

  
    app_window.mainloop()

it's still incomplete, but i hope you get idea, i want to pass the return of the get_datos function, but i don't know how to do that, if any advice on how to tackle this, would be welcome.

otto
  • 1
  • 2

1 Answers1

0

Here is very simplified version of what I did when I needed to bring some values outside of Tkinter environment:

import tkinter


class Window:
    def __init__(self, root, variable):
        self.root = root
        self.variable = variable
        self.root.mainloop()
        self.onClose()

    def onClose(self):
        self.outVariable = variable + 1

    @classmethod
    def getVariableFromWindow(cls, variable):
        root = tkinter.Tk()
        window = cls(root, variable)
        return window.outVariable

if __name__ == "__main__":
    variable = 3
    variable = Window.getVariableFromWindow(3)
    print(variable)

In your case, you would save (bancos,montos_a_depositar,envasadoras) as attributes of the object and then return those when the main loop is exited.

UPDATE: It seems like you don't really need Flask, but only the HTML it lets you render with a template. Flask uses Jinja2 to do that, and you can use it directly instead of spinning up a web app to do so, as described in this answer: render jinja2 template without a Flask context. This way you can provide variables obtained from GUI directly into render_jinja_html and get complete HTML as a return value which you can then work on converting into PDF, for example as described here: Geeks for Geeks - Python Convert Html to PDF

matszwecja
  • 6,357
  • 2
  • 10
  • 17
  • ok i get that, but how do a pass that info to the flask render funtion? since that function is defined and decorated by the flask app. – otto Apr 20 '22 at 13:03
  • Do you want both UI and Flask running simultaneously? – matszwecja Apr 20 '22 at 13:10
  • no, i was assuming i could use a funtion to render, the HTML (there's a button to generate the PDF) and pass the info gathered through the UI – otto Apr 20 '22 at 13:21
  • If you are trying to run Tkinter UI client-side, that's pretty much impossible. – matszwecja Apr 20 '22 at 13:28
  • so i can't store the data in a variable and pass it to render somehow? – otto Apr 20 '22 at 13:33
  • To clarify, you want the UI to show up client-side, not server-side? So, **not** on a machine that is running the app? – matszwecja Apr 20 '22 at 13:35
  • what i want is the following, after the user finishes to enter all data, push "Generate PDF", that button will call the flask and pass all the data as arguments, then render the HTML and turn it into a PDF that is going to be saved locally. – otto Apr 20 '22 at 14:03
  • @otto See update – matszwecja Apr 21 '22 at 09:01
  • waoo, thats great, thanks alot – otto Apr 21 '22 at 15:57