Question Thread watched - - - What is a "Weak Reference" and where am I using it ?

PrabhakaranKaruppaih

Distinguished
May 18, 2016
182
2
18,585
Python:
from tkinter import *
import pymysql
import mysql.connector
connection = pymysql.connect(host="shafty",
                             user="root",
                             password="abc123",
                             database="Mubarak")
cursor = connection.cursor()
MainWindow = Tk()
MainWindow.title("Main Form")
NameLabel = Label(MainWindow,text="Name",width=7,height=1,anchor="nw")
NameLabel.grid(row=1,column=1)
NameBox = Entry(MainWindow,width=20)
NameBox.grid(row=1,column=2)
AgeLabel = Label(MainWindow,text="Age",width=7,height=1,anchor="nw")
AgeLabel.grid(row=2,column=1)
AgeBox = Entry(MainWindow,width=20)
AgeBox.grid(row=2,column=2)
LocationLabel = Label(MainWindow,text="Location",anchor="nw")
LocationLabel.grid(row=3,column=1)
LocationBox = Entry(MainWindow,width=20)
LocationBox.grid(row=3,column=2)
def PopulateGrid():
    cursor.execute("SELECT * FROM Persons")  
    ReportData = cursor.fetchall()
    import pandas as pd
    ReportFrame = pd.DataFrame(ReportData)
    Report = ReportFrame.to_html(index=False,header=False)
    from tkinterweb import HtmlFrame
    web_frame=HtmlFrame(MainWindow)
    web_frame.grid(row=5,column=2)
    web_frame.load_html(Report)

    from jinja2 import Environment, Template
    env = Environment(loader='file')
    myTemplate = env.get_template("Table.html")
    html_table = ReportFrame.to_html(index=False)
    html_content = myTemplate.render(table_data=html_table)
def Upload():  
    cursor.execute("INSERT INTO Persons (NameVal, Age, Location) values (%s, %s, %s)",
                   (NameBox.get(), AgeBox.get(), LocationBox.get()))
    connection.commit()
    print("SuccessFully Updated!")
    PopulateGrid()
    import tkinter.messagebox as messagebox
    messagebox.showinfo(title="Button Clicked", message="Successfully Uploaded!")
UploadButton = Button(MainWindow,text="Upload",anchor="nw",command=Upload)
UploadButton.grid(row=4,column=2)
PopulateGrid()
MainWindow.mainloop()

I am getting "cannot create weak reference to 'str' object" at the below sentence:
myTemplate = env.get_template("Table.html")
How to avoid it in future? Please explain. Thank You.
 

Ralston18

Titan
Moderator
My guess is this line is causing problems
Python:
env = Environment(loader='file')

All of the other code examples and people using this use I found either pass in an instance of a FileSystemLoader or PackageLoader from the jninja2 package. Otherwise refer to the documentation to see what it's expecting.

The error specifically though seems to suggest somewhere in get_template it calls weakref.ref. And, at least for the CPython reference implementation, you can't make weak references for strings as, to avoid overhead costs, built-in types don't have space for the weakref mechanism [ref].