64
64
import threading # threaded popup
65
65
from datetime import date , datetime
66
66
from time import sleep , time # threaded popup
67
- from typing import Callable , Dict , List , Optional , Tuple , Type , TypedDict , Union # docs
67
+ from typing import Any , Callable , Dict , List , Optional , Tuple , Type , TypedDict , Union
68
68
69
+ import pandas as pd
69
70
import PySimpleGUI as sg
70
71
71
72
# Wrap optional imports so that pysimplesql can be imported as a single file if desired:
@@ -317,7 +318,7 @@ def parent_virtual(cls, table: str, frm: Form) -> Union[bool, None]:
317
318
for r in cls .instances :
318
319
if r .child_table == table and r .on_update_cascade :
319
320
try :
320
- return frm [r .parent_table ].get_current_row (). virtual
321
+ return frm [r .parent_table ].row_is_virtual ()
321
322
except AttributeError :
322
323
return False
323
324
return None
@@ -541,7 +542,7 @@ def __init__(
541
542
self .search_order : List [str ] = []
542
543
self .selector : List [str ] = []
543
544
self .callbacks : CallbacksDict = {}
544
- self .transform : Optional [Callable [[ResultRow , int ], None ]] = None
545
+ self .transform : Optional [Callable [[pd . DataFrame , int ], None ]] = None
545
546
self .filtered : bool = filtered
546
547
if prompt_save is None :
547
548
self ._prompt_save = self .frm ._prompt_save
@@ -810,7 +811,7 @@ def records_changed(self, column: str = None, recursive=True) -> bool:
810
811
logger .debug (f'Checking if records have changed in table "{ self .table } "...' )
811
812
812
813
# Virtual rows wills always be considered dirty
813
- if self .row_is_virtual (self . current_index ):
814
+ if self .row_is_virtual ():
814
815
return True
815
816
816
817
dirty = False
@@ -1358,7 +1359,6 @@ def set_by_pk(
1358
1359
# don't update self/dependents if we are going to below anyway
1359
1360
self .prompt_save (update_elements = False )
1360
1361
1361
- print ("index:" , self .rows .index [self .rows [self .pk_column ] == pk ].tolist ()[0 ])
1362
1362
self .current_index = self .rows .index [self .rows [self .pk_column ] == pk ].tolist ()[
1363
1363
0
1364
1364
]
@@ -1384,7 +1384,6 @@ def get_current(
1384
1384
logger .debug (f"Getting current record for { self .table } .{ column } " )
1385
1385
if len (self .rows .index ):
1386
1386
if self .get_current_row ()[column ]:
1387
- print ("Current: " , self .get_current_row ()[column ])
1388
1387
return self .get_current_row ()[column ]
1389
1388
return default
1390
1389
return default
@@ -1666,7 +1665,7 @@ def save_record(
1666
1665
self .driver .rollback ()
1667
1666
return SAVE_FAIL # Do not show the message in this case
1668
1667
else :
1669
- if self .row_is_virtual (self . current_index ):
1668
+ if self .row_is_virtual ():
1670
1669
result = self .driver .insert_record (
1671
1670
self .table , self .get_current_pk (), self .pk_column , changed_row
1672
1671
)
@@ -1704,7 +1703,7 @@ def save_record(
1704
1703
self .frm [self .table ].requery_dependents ()
1705
1704
1706
1705
# Lets refresh our data
1707
- if self .row_is_virtual (self . current_index ):
1706
+ if self .row_is_virtual ():
1708
1707
# Requery so that the new row honors the order clause
1709
1708
self .requery (select_first = False , update_elements = False )
1710
1709
if update_elements :
@@ -1810,7 +1809,7 @@ def delete_record(
1810
1809
if answer == "no" :
1811
1810
return True
1812
1811
1813
- if self .row_is_virtual (self . current_index ):
1812
+ if self .row_is_virtual ():
1814
1813
self .rows .purge_virtual ()
1815
1814
self .frm .update_elements (self .key )
1816
1815
# only need to reset the Insert button
@@ -1860,7 +1859,7 @@ def duplicate_record(
1860
1859
:returns: None
1861
1860
"""
1862
1861
# Ensure that there is actually something to duplicate
1863
- if not len (self .rows .index ) or self .row_is_virtual (self . current_index ):
1862
+ if not len (self .rows .index ) or self .row_is_virtual ():
1864
1863
return None
1865
1864
1866
1865
# callback
@@ -1970,13 +1969,16 @@ def get_description_for_pk(self, pk: int) -> Union[str, int, None]:
1970
1969
return row [self .description_column ]
1971
1970
return None
1972
1971
1973
- def row_is_virtual (self , index : int ) -> bool :
1972
+ def row_is_virtual (self , index : int = None ) -> bool :
1974
1973
"""
1975
1974
Check whether the row at `index` is virtual
1976
1975
1977
- :param index: The index to check
1976
+ :param index: The index to check. If none is passed, then the current index will
1977
+ be used.
1978
1978
:returns: True or False based on whether the row is virtual
1979
1979
"""
1980
+ if index is None :
1981
+ index = self .current_index
1980
1982
return self .rows .attrs ["virtual" ][index ]
1981
1983
1982
1984
def table_values (
@@ -3138,7 +3140,7 @@ def update_elements(
3138
3140
# this is a virtual row
3139
3141
marker_key = mapped .element .key + ":marker"
3140
3142
try :
3141
- if mapped .dataset .get_current_row (). virtual :
3143
+ if mapped .dataset .row_is_virtual () :
3142
3144
# get the column name from the key
3143
3145
col = mapped .column
3144
3146
# get notnull from the column info
@@ -3204,14 +3206,6 @@ def update_elements(
3204
3206
lst = []
3205
3207
print (type (target_table ), target_table )
3206
3208
for index , row in target_table .rows .iterrows ():
3207
- print (row )
3208
- print (
3209
- row ,
3210
- pk_column ,
3211
- description ,
3212
- row [pk_column ],
3213
- row [description ],
3214
- )
3215
3209
lst .append (ElementRow (row [pk_column ], row [description ]))
3216
3210
3217
3211
# Map the value to the combobox, by getting the description_column
@@ -5834,22 +5828,18 @@ def _get_list(self, key: str) -> List:
5834
5828
# return a generic ResultSet instance, which contains a collection of generic ResultRow
5835
5829
# instances.
5836
5830
# --------------------------------------------------------------------------------------
5837
-
5838
-
5839
- import pandas as pd
5840
-
5841
-
5842
5831
class ResultSet (pd .DataFrame ):
5843
5832
"""
5844
5833
The ResultSet class is a generic result class so that working with the resultset of
5845
5834
the different supported databases behave in a consistent manner. A `ResultSet` is a
5846
5835
Pandas dataframe with some extra functionality to make working with abstracted
5847
5836
database drivers easier.
5848
5837
5849
- ResultSets can be thought up as rows of information. Iterating through a ResultSet
5838
+ ResultSets can be thought up as rows of information and are build from a Pandas
5839
+ DataFrame. Iterating through a ResultSet
5850
5840
is very simple:
5851
5841
ResultSet = driver.execute('SELECT * FROM Journal;')
5852
- for row in rows:
5842
+ for index, row in rows.iteritems() :
5853
5843
print(row['title'])
5854
5844
5855
5845
Note: The lastrowid is set by the caller, but by pysimplesql convention, the
0 commit comments