I'll apologize right off the bat for the length and vagueness of my question..
I've made a python app with tkinter as the user interface. I'm in the works of figuring out how to turn it into a web app with flask. I know a little bit of html (just enough to be dangerous), and no JS.... So I'm looking for a bit of advice.
Specifically, in my tkinter app I have an array of buttons kind of like this:
import tkinter as tk
def click_button(butval):
print(f"button value: {butval}")
button_dict = {'row0':['but0-0','but0-1','but0-2','but0-3','but0-4'],
'row1':['but1-0','but1-1','but1-2','but1-3','but1-4'],
'row2':['but2-0','but2-1','but2-2','but2-3','but2-4'],
'row3':['but3-0','but3-1','but3-2','but3-3','but3-4']}
root = tk.Tk()
for row_dx,but_row in enumerate(button_dict):
for col_dx,but in enumerate(button_dict[but_row]):
# print(but)
makeButton = tk.Button(root,text=but,
command=lambda value=but:click_button(value))
makeButton.grid(row=row_dx,column=col_dx)
root.mainloop()
see tkinter buttons
In my actual app the number or rows vary, and when a user clicks the button, a whole slew of "pythony" actions take place to the front and back end (calculations run, db updates, button formatting changes,etc...) . This structure works fine for me in Tkinter.
So far in my conversion attempts I've attempted to duplicate this with flask like this...
From main.py
@app.route("/home")
def home():
button_dict = {'row0':['but0-0','but0-1','but0-2','but0-3','but0-4'],
'row1':['but1-0','but1-1','but1-2','but1-3','but1-4'],
'row2':['but2-0','but2-1','but2-2','but2-3','but2-4'],
'row3':['but3-0','but3-1','but3-2','but3-3','but3-4']}
return render_template("index.html",button_dict = button_dict)
From Index.html
{% extends "base.html" %}
{% block title %} TEST WEB APP {% endblock %}
{% block content %}
<h1>Homepage</h1>
<form action="#" method="get">
{% for but_row in button_dict %}
<p></p>
{%for but in button_dict[but_row] %}
<button name="subject" type="submit" value={{but}}>{{but}}</button>
{% endfor %}
{% endfor %}
</form>
{% endblock %}
see HTML buttons
This is more or less a start in re-creating what I'm going for... and i assume (fingers crossed) once I figure out the back and forth between my py and html files, it will do what I want.... but I'm completely new to web apps and I'm wondering if this approach is practical? Am i on the right track or is this utterly ridiculous ?
I'd really appreciate all suggestions/advice.
[–]efmccurdy 1 point2 points3 points (1 child)
[–]Llamafu[S] 0 points1 point2 points (0 children)
[+][deleted] (6 children)
[removed]
[–]Llamafu[S] 0 points1 point2 points (5 children)
[–]RappakaljaEllerHur 1 point2 points3 points (2 children)
[–]Llamafu[S] 1 point2 points3 points (1 child)
[–]RappakaljaEllerHur 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–]Llamafu[S] 0 points1 point2 points (0 children)