Skip to content

Commit

Permalink
Merge pull request #40 from jordantgh/tags_pane
Browse files Browse the repository at this point in the history
Tags pane

Added tags pane to the processed/prunes data info view

Adds tags column to the central metadata tables of the dbs
  • Loading branch information
jordantgh authored Dec 19, 2023
2 parents e3593b6 + 5678318 commit 14c5d13
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 50 deletions.
53 changes: 41 additions & 12 deletions app/controller/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,15 @@ def _connect_sigs(
parsed.article_ui_list.itemClicked: self.on_article_clicked,
parsed.filter_sig: self.filter_tables,
parsed.prune_sig: self.prune_tables_and_columns,
parsed.tags_entry_widget.tagAdded: self.add_tag,
parsed.tags_display_widget.tagRemoved: self.remove_tag,

# Pruned page
pruned.article_ui_list.itemClicked: self.on_article_clicked,
pruned.filter_sig: self.filter_tables,
pruned.prune_sig: self.prune_tables_and_columns,
pruned.tags_entry_widget.tagAdded: self.add_tag,
pruned.tags_display_widget.tagRemoved: self.remove_tag,

# Search threads
search_thread.article_sig: self.display_article_in_list,
Expand All @@ -98,6 +102,20 @@ def _connect_sigs(
for signal, slot in signals_map.items():
signal.connect(slot)
self.signal_connections.append((signal, slot))

def add_tag(self, tag: 'str'):
self.model.last_selected_table.add_tag(tag)

self.curr_elems.tags_display_widget.clear()
for tag in self.model.last_selected_table.get_tags():
self.curr_elems.tags_display_widget.addTag(tag)

def remove_tag(self, tag: 'str'):
self.model.last_selected_table.remove_tag(tag)

self.curr_elems.tags_display_widget.clear()
for tag in self.model.last_selected_table.get_tags():
self.curr_elems.tags_display_widget.addTag(tag)

def _disconnect_sigs(self):
for signal, slot in self.signal_connections:
Expand All @@ -121,14 +139,15 @@ def display_article_in_list(
def search_articles(self):
self._set_state(Mode.SEARCHING)
self.model.reset_for_searching()
self.view.tab_widget.setCurrentIndex(0)
self.view.tabbed_pageholder.setCurrentIndex(0)

if self.model.search_thread.isRunning():
QMessageBox.warning(
self.view,
"Search in Progress",
"A search is already in progress. "
"Please wait or stop the current search.")
"Please wait or stop the current search."
)
return

query = self.output_page.query_field.text()
Expand Down Expand Up @@ -179,13 +198,18 @@ def on_article_clicked(self, item: 'QListWidgetItem'):
self.view.update_article_display(article, elements, data_set)

for i in range(elements.data_ui_list.count()):
list_item = elements.data_ui_list.item(i)
widget: 'DataListItem' = elements.data_ui_list.itemWidget(
list_item)
data_list_item: 'DataListItem' = elements.data_ui_list.itemWidget(
elements.data_ui_list.item(i)
)

if elements.page_identity == PageIdentity.SEARCH:
widget.preview_requested.connect(self.request_suppfile_preview)
data_list_item.preview_requested.connect(
self.request_suppfile_preview
)
else:
widget.preview_requested.connect(self.preview_processed_table)
data_list_item.preview_requested.connect(
self.preview_processed_table
)

def load_preview(
self,
Expand Down Expand Up @@ -217,7 +241,7 @@ def load_preview(
self.view.stop_load_animation()

# The original signal emits two arguments, but this slot only takes one
# - why does this work? Granted, we don't need the context argument, but
# - why does this work? Granted, we don't *need* the context argument, but
# still, it doesn't make sense why this doesn't throw an error.
# TODO debug later
def request_suppfile_preview(self, file_data: 'SuppFile'):
Expand All @@ -238,6 +262,12 @@ def request_suppfile_preview(self, file_data: 'SuppFile'):
def preview_processed_table(
self, table: 'ProcessedTable', context: 'PageIdentity'
):
# hacky to do this here, should have own method
self.model.last_selected_table = table
self.curr_elems.tags_display_widget.clear()
for tag in table.get_tags():
self.curr_elems.tags_display_widget.addTag(tag)

table_data = {
"sheet": self.model.table_db_manager.get_processed_table_data(
table.id, context
Expand Down Expand Up @@ -409,11 +439,11 @@ def save(self):
self.model.saves_path,
f"session-{idx}-{datetime.now().strftime('%Y%m%d-%H%M%S')}.pkl"
)

if os.path.exists(self.model.session_file):
os.remove(self.model.session_file)
else:
idx = len(os.listdir(self.model.saves_path)) +1
idx = len(os.listdir(self.model.saves_path)) + 1
filepath = os.path.join(
self.model.saves_path,
f"session-{idx}-{datetime.now().strftime('%Y%m%d-%H%M%S')}.pkl"
Expand Down Expand Up @@ -443,14 +473,13 @@ def load(self):
if not filename:
return


# repopulate the GUI
# clear all pages

# re-init
self.model.table_db_manager.delete_dbs()
self._disconnect_sigs()

self.model.load(filename)
self.view.reset()

Expand Down
12 changes: 12 additions & 0 deletions app/model/article_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def __init__(
self.supp_file: 'SuppFile' = article.get_file(file_id)
self.pruned_columns: 'list[int]' = []
self.observers: 'dict[PageIdentity, DataListItem]' = {}
self.tags = []

def checkbox_toggled(self, context: 'PageIdentity'):
self.notify_observers(context)
Expand All @@ -107,6 +108,17 @@ def set_checked_state(self, checked_state: 'bool', context: 'PageIdentity'):
if context in self.observers:
self.notify_observers(context)

def add_tag(self, tag: 'str'):
if tag not in self.tags:
self.tags.append(tag)
# self.notify_observers(PageIdentity.PRUNED)

def remove_tag(self, tag: 'str'):
if tag in self.tags:
self.tags.remove(tag)

def get_tags(self):
return self.tags

class ProcessedTableManager:
def __init__(self):
Expand Down
27 changes: 15 additions & 12 deletions app/model/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from sqlalchemy import create_engine, Column, String, text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.dialects.sqlite import JSON
from sqlalchemy.orm import sessionmaker
from uuid import uuid4
import pandas as pd
Expand All @@ -21,7 +22,7 @@ class TableDBEntry(Base):
__abstract__ = True
table_id = Column(String, primary_key=True)
original_file_id = Column(String)
sql_table_name = Column(String)
tags = Column(JSON)


class ProcessedTableDBEntry(TableDBEntry):
Expand Down Expand Up @@ -90,16 +91,16 @@ def save_table(
table_class: 'TableDBEntry',
table_id: 'str',
original_file_id: 'UUID',
df: 'DataFrame'
df: 'DataFrame',
tags: 'list[str]' = []
):
engine, Session = self._get_engine_and_session(table_class)
with Session() as session:
sql_table_name = f"{table_class.__tablename__}_{table_id}"
df.to_sql(sql_table_name, engine, index=False)
df.to_sql(table_id, engine, index=False)
new_table = table_class(
table_id=table_id,
original_file_id=str(original_file_id),
sql_table_name=sql_table_name
tags=tags
)
session.add(new_table)
session.commit()
Expand All @@ -118,7 +119,7 @@ def update_table(

if existing_table:
df.to_sql(
existing_table.sql_table_name,
existing_table.table_id,
engine,
if_exists='replace',
index=False
Expand Down Expand Up @@ -146,7 +147,7 @@ def get_table_data(
table_id=table_id
).first()
if table_entry:
df = pd.read_sql_table(table_entry.sql_table_name, engine)
df = pd.read_sql_table(table_entry.table_id, engine)
return df.reset_index(drop=True)
return None

Expand All @@ -173,7 +174,7 @@ def delete_table(
if table_entry:
with engine.connect() as conn:
conn.execute(
text(f"DROP TABLE IF EXISTS \"{table_entry.sql_table_name}\""))
text(f"DROP TABLE IF EXISTS \"{table_entry.table_id}\""))
session.delete(table_entry)
session.commit()

Expand All @@ -183,7 +184,7 @@ def save_dbs(self) -> 'list[str]':

processed_target_path = self.old_fnames[0]
pruned_target_path = self.old_fnames[1]

# Delete old versions of the databases
if os.path.exists(processed_target_path):
os.remove(processed_target_path)
Expand Down Expand Up @@ -224,7 +225,7 @@ def reset(self):
for entry in table_entries:
with engine.connect() as conn:
conn.execute(
text(f"DROP TABLE IF EXISTS \"{entry.sql_table_name}\""))
text(f"DROP TABLE IF EXISTS \"{entry.table_id}\""))
session.query(table_class).delete()
session.commit()

Expand All @@ -245,11 +246,13 @@ def processed_df_to_db(
db_manager: 'TableDBManager',
table_id: 'str',
original_file_id: 'UUID',
df: 'DataFrame'
df: 'DataFrame',
tags: 'list[str]'
):
db_manager.save_table(
ProcessedTableDBEntry,
table_id,
original_file_id,
df
df,
tags
)
4 changes: 3 additions & 1 deletion app/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(
self.table_db_manager = TableDBManager(db_temp_path, db_perm_path)
self.processed_table_manager = ProcessedTableManager()
self.processing_thread = FileProcessingThread(self.table_db_manager)
self.last_selected_table: 'ProcessedTable' = None
self.n_parse_runs = 0
self.n_prunes = 0

Expand Down Expand Up @@ -148,7 +149,8 @@ def prune_tables_and_columns(self, context: 'PageIdentity'):
PostPruningTableDBEntry,
table.id,
table.file_id,
pruned_df
pruned_df,
table.tags
)

article.pruned_tables = selected_tables
Expand Down
2 changes: 1 addition & 1 deletion app/model/tabular_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def parse_tables(
)

processed_df_to_db(
db_manager, unique_id, file.id, data
db_manager, unique_id, file.id, data, []
)

processed_table_ids.append((unique_id, file.id))
Expand Down
Loading

0 comments on commit 14c5d13

Please sign in to comment.