mongodb documents values in treeview in python - python

I want to display mongodb documents values in treeview in python.
Below is my code, but it not displays data in proper manner in treeview.
collection = myDB['students']
rows = list(collection.find())
if len(rows)!=0:
self.student_table.delete(*self.student_table.get_children())
for row in rows:
self.student_table.insert("",END,values=row.values())

You need to change row.values() (type dict_values) to list:
for row in rows:
self.student_table.insert("", END, values=list(row.values()))

Related

how to add headers when selecting items from an SQL table?

so my sql query quite simply just selects all of the items in the table and prints each entry one under the other. I am wondering if there is a way to get headers above each of the items so the user can tell what each item in the table means here is my current query if needed:
c.execute("SELECT * FROM outflows1")
data = c.fetchall()
print(data)
print("Currently in database:")
for row in data:
print(row)
conn.commit()
so this query outputs all of the items in my database however I am wondering if there is a way to put headers above the output which label what each of the items are. Thanks

Python docxtpl - How to change the size of the column?

I am using docxtpl to generate word documents. I am inserting multiple tables to a Word document and wondering how I can change the size of the specific column in the code.
Here is the table in my template.docx:
The table looks in the report like in the image:
Here is a glance how I insert the table to the docx:
## Create panda data frame from the dict
df_bauwerke = pd.DataFrame.from_dict(outDict['Bauwerke']['Schlüsselbauwerke'], orient='index')
## Convert panda df to DocxTemplate table format
table_bauwerke = {
"bauwerke_col_labels": list(df_bauwerke.columns.values),
"bauwerke_tbl_contents": [{"cols": rows} for rows in df_bauwerke.values.tolist()]}
context_to_load.update(table_bauwerke)
I would like to change the width of column 'Jahr' & 'Name' (Name is larger and Jahr is narrower) and the rest stay as they are. Can I influence that in my script?

display data from sqlite database in listbox and use id to delete it

I have a table in my db with following output when I fetch all lines:
(1, 'Type in your Note', '1592897997.43793')
(2, 'Type in your Note', '1592898130.55058')
(3, 'Type in your Note', '1592898180.61336')
(4, 'Type in your Note', '1592898180.83751')
(5, 'Type in your Note', '1592898222.27298')
(6, 'Type in your Note', '1592898222.43869')
in the fetch all method i insert them to the listboy as following
for row in rows:
print(row)
listbox.insert(END, row)
The output in the Listbox is the following. I want just the text to be in the listbox and I need the ID to delete the single Items. Does someone have an idea how i can do that?
I'm guessing that you get the data from an Sql-Statement. Hence if you want to specifiy the content put into your listbox I would edit that Sql-Statement.
Instead of selecting everything from the Sqllite Databse:
rows=con.execute(„SELECT * FROM example“)
You select only the thing you need:
rows=con.execute(„SELECT text FROM example“)
After that your current code should work fine.
for row in rows:
print(row)
listbox.insert(END, row)
To delete the selected object create a seperate list like #acw1668 described
Then you need to create a list to store the IDs (with same order as
Listbox). When removing item from the Listbox, get the ID in the ID
list (based on the index of selected item in Listbox) to remove record
in the table and delete the ID from the ID list
You can create a list that maps listbox indexes to ids. It would look something like this:
db_ids = []
for row in rows:
(db_id, text, something_else) = row
listbox.insert(END, text)
db_ids.append(db_id)
With that, you can get any database id by index (eg: db_ids[0], db_ids[1], etc)
You must take care to remove corresponding items from the list when you remove items from the listbox.

petl convert data from duplicate entries

Am trying to use petl library to build an ETL process that copied data between two tables. The table contain a unique slug field on the destination. For that, I wrote my script so It would identify duplicate slugs and convert them with by appending ID to the slug value.
table = etl.fromdb(source_con, 'SELECT * FROM user')
# get whatever remains as duplicates
duplicates = etl.duplicates(table, 'slug')
for dup in [i for i in duplicates.values('id')]:
table = etl.convert(
table,
'slug',
lambda v, row: '{}-{}'.format(slugify_unicode(v), str(row.id).encode('hex')),
where=lambda row: row.id == dup,
pass_row=True
)
The above did not work as expected, it seems like the table object remains with duplicate values after the loop.
Anyone can advise?
Thanks

How can I format my data in qgridlayout or similar widgets?

def view(self):
sender=self.sender()
self.statusBar().showMessage(sender.text()+' was pressed.')
with sqlite3.connect('drycleaning_shop.db') as db:
cursor=db.cursor()
cursor.execute('select* from Item Order BY Name ASC')
product=cursor.fetchall()
product=str(product)
print(len(product))
self.l.setText(str(product))
self.l124.setText(view2)
This is what is representing my data at the moment, the data is printed into a label (l124) and is printed through a single line. I want the qgrid to have my data represented in a table of some sort organised in rows and columns.

Categories