implemented neo4j example

This commit is contained in:
Denis Volkov 2025-04-14 22:20:31 +03:00
parent b1a4c5fbbf
commit 5505a521ff
4 changed files with 101 additions and 4 deletions

9
.env Normal file
View File

@ -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

View File

@ -3,5 +3,17 @@
Используется:
- PyQt5
- PostgresSQL (psycopg2)
- Neo4j
![form.png](form.png)
Postgres:
![form.png](form.png)
Neo4j:
![neo_form.png](neo_form.png)
### Подключение к БД
Для подключения к БД указать в .env необходимые данные.
Для выборки данных из Neo4j должен быть создан обучающий граф фильмов.

82
main.py
View File

@ -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())

BIN
neo_form.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB