diff --git a/.env b/.env new file mode 100644 index 0000000..cddec1c --- /dev/null +++ b/.env @@ -0,0 +1,9 @@ +POSTGRES_USER = postgres +POSTGRES_DB = university +POSTGRES_PASS = pass +POSTGRES_HOST = localhost +POSTGRES_PORT = 5432 + +NEO_USER=neo4j +NEO_PASS=pass +NEO_HOST=neo4j://localhost:7687 \ No newline at end of file diff --git a/README.md b/README.md index 5de7007..5e42353 100644 --- a/README.md +++ b/README.md @@ -3,5 +3,17 @@ Используется: - PyQt5 - PostgresSQL (psycopg2) +- Neo4j -![form.png](form.png) \ No newline at end of file +Postgres: + +![form.png](form.png) + +Neo4j: + +![neo_form.png](neo_form.png) + +### Подключение к БД + +Для подключения к БД указать в .env необходимые данные. +Для выборки данных из Neo4j должен быть создан обучающий граф фильмов. \ No newline at end of file diff --git a/main.py b/main.py index 9c3a7e1..41f931f 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,24 @@ import psycopg2 from psycopg2 import sql from psycopg2.errors import DuplicateDatabase -from PyQt5.QtWidgets import QApplication, QMainWindow, QGridLayout, QWidget, QTableWidget, QTableWidgetItem, QHeaderView +from neo4j import GraphDatabase +from PyQt5.QtWidgets import QApplication, QMainWindow, QGridLayout, QWidget, QTableWidget, QTableWidgetItem, \ + QHeaderView, QVBoxLayout, QLabel from PyQt5.QtCore import QSize, Qt +import os +from dotenv import load_dotenv +load_dotenv() + +NEO_USER = os.getenv('NEO_USER') +NEO_PASS = os.getenv('NEO_PASS') +NEO_HOST = os.getenv('NEO_HOST') + +POSTGRES_USER = os.getenv('POSTGRES_USER') +POSTGRES_DB = os.getenv('POSTGRES_DB') +POSTGRES_PASS = os.getenv('POSTGRES_PASS') +POSTGRES_HOST = os.getenv('POSTGRES_HOST') +POSTGRES_PORT = os.getenv('POSTGRES_PORT') def connect(config): """ Connect to the PostgreSQL database server """ @@ -97,7 +112,7 @@ class MainWindow(QMainWindow): QMainWindow.__init__(self) self.setMinimumSize(QSize(480, 80)) # Set sizes - self.setWindowTitle("DB example") # Set the window title + self.setWindowTitle("postgre") # Set the window title central_widget = QWidget(self) # Create a central widget self.setCentralWidget(central_widget) # Install the central widget @@ -136,6 +151,54 @@ class MainWindow(QMainWindow): self.table.setItem(row_number, 2, QTableWidgetItem(str(user[2]))) +class NeoWindow(QWidget): + def __init__(self, parent_window): + super().__init__() + + self.setWindowTitle("neo4j") + self.resize(300, 250) + + grid_layout = QGridLayout(self) # Create QGridLayout + self.setLayout(grid_layout) # Set this layout in central widget + + self.table = QTableWidget(self) + self.table.setColumnCount(1) + self.table.setRowCount(1) + + self.table.setHorizontalHeaderLabels(["Info"]) + + # Set the tooltips to headings + self.table.horizontalHeaderItem(0).setToolTip("Info") + + # Set the alignment to the headers + self.table.horizontalHeaderItem(0).setTextAlignment(Qt.AlignHCenter) + + + grid_layout.addWidget(self.table, 0, 0) # Adding the table to the grid + + # Position the window to the right of the parent window + self.position_relative_to_parent(parent_window) + + def position_relative_to_parent(self, parent_window): + # Get the geometry of the parent window + parent_frame = parent_window.frameGeometry() + + # Calculate new position (right of parent window with a small gap) + new_x = parent_frame.x() + parent_frame.width() + 5 + new_y = parent_frame.y() + + # Move the window to the new position + self.move(new_x, new_y) + + def load_data(self, neo_data): + for i, node_data in enumerate(neo_data): + row_number = self.table.rowCount()-1 + if len(neo_data) != self.table.rowCount(): + self.table.insertRow(row_number) + self.table.setItem(row_number, 0, QTableWidgetItem(str(node_data[0]["title"]))) + + self.table.resizeColumnsToContents() + if __name__ == "__main__": import sys @@ -143,7 +206,8 @@ if __name__ == "__main__": createuUiversityDB(conn) - conn = psycopg2.connect("user=postgres dbname=university password=456 host=localhost port=5432") + conn = psycopg2.connect("user={0} dbname={1} password={2} host={3} port={4}".format(POSTGRES_USER, POSTGRES_DB, + POSTGRES_PASS, POSTGRES_HOST, POSTGRES_PORT)) createUsersTable(conn) @@ -157,4 +221,16 @@ if __name__ == "__main__": mw = MainWindow() mw.load_data(users) mw.show() + + URI = NEO_HOST + AUTH = (NEO_USER, NEO_PASS) + + with GraphDatabase.driver(URI, auth=AUTH) as driver: + records, summary, keys = driver.execute_query( + "MATCH (nineties:Movie) WHERE nineties.released >= 1990 AND nineties.released < 2000 RETURN nineties" + ) + + neo_window = NeoWindow(mw) + neo_window.load_data(records) + neo_window.show() sys.exit(app.exec()) \ No newline at end of file diff --git a/neo_form.png b/neo_form.png new file mode 100644 index 0000000..eab1d9d Binary files /dev/null and b/neo_form.png differ