parent
bbb33fc77f
commit
43dbe63336
Binary file not shown.
File diff suppressed because one or more lines are too long
@ -1,67 +0,0 @@
|
||||
"""
|
||||
pip install asyncpg
|
||||
"""
|
||||
import asyncpg
|
||||
|
||||
from Config.Config import *
|
||||
|
||||
# PostgreSQL 配置
|
||||
POSTGRES_CONFIG = {
|
||||
"host": POSTGRES_HOST,
|
||||
"port": POSTGRES_PORT,
|
||||
"user": POSTGRES_USER,
|
||||
"password": POSTGRES_PASSWORD,
|
||||
"database": POSTGRES_DATABASE,
|
||||
"min_size": 1, # 设置为0表示不保留空闲连接
|
||||
"max_size": 20,
|
||||
"command_timeout": 60
|
||||
}
|
||||
|
||||
# 初始化 PostgreSQL 连接池
|
||||
async def init_postgres_pool():
|
||||
return await asyncpg.create_pool(**POSTGRES_CONFIG)
|
||||
|
||||
|
||||
# 查询示例
|
||||
async def query_example(pool):
|
||||
async with pool.acquire() as conn:
|
||||
# 执行查询
|
||||
rows = await conn.fetch('SELECT * FROM your_table LIMIT 10')
|
||||
# 处理结果
|
||||
for row in rows:
|
||||
print(dict(row))
|
||||
return rows
|
||||
|
||||
|
||||
# 插入示例
|
||||
async def insert_example(pool, data):
|
||||
async with pool.acquire() as conn:
|
||||
# 执行插入
|
||||
stmt = """
|
||||
INSERT INTO your_table (column1, column2)
|
||||
VALUES ($1, $2)
|
||||
"""
|
||||
# 使用参数化查询防止SQL注入
|
||||
inserted_id = await conn.fetchval(stmt, data['value1'], data['value2'])
|
||||
print(f"插入成功!")
|
||||
return inserted_id
|
||||
|
||||
|
||||
# 使用示例
|
||||
async def main():
|
||||
pool = await init_postgres_pool()
|
||||
|
||||
# 查询示例
|
||||
await query_example(pool)
|
||||
|
||||
# 插入示例
|
||||
#data_to_insert = {'value1': 'test1', 'value2': 'test2'}
|
||||
#await insert_example(pool, data_to_insert)
|
||||
|
||||
await pool.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import asyncio
|
||||
|
||||
asyncio.run(main())
|
Binary file not shown.
Binary file not shown.
@ -1,37 +0,0 @@
|
||||
from pyvis.network import Network
|
||||
import json
|
||||
from selenium import webdriver
|
||||
import time
|
||||
|
||||
# 读取您的JSON知识图谱数据
|
||||
with open(r'D:\dsWork\dsProject\dsLightRag\Doc\史校长资料\技术一-知识图谱源文件\middle_school_math_graph.json',encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
|
||||
# 创建网络图
|
||||
net = Network(height='800px', width='100%', notebook=False)
|
||||
|
||||
# 添加节点
|
||||
for node in data['nodes']:
|
||||
net.add_node(node['id'], label=node['id'][:20]+'...' if len(node['id'])>20 else node['id'])
|
||||
|
||||
# 添加边(如果有的话)
|
||||
if 'edges' in data:
|
||||
for edge in data['edges']:
|
||||
net.add_edge(edge['source'], edge['target'])
|
||||
|
||||
# 保存为HTML文件
|
||||
html_path = 'knowledge_graph_json.html'
|
||||
net.show(html_path)
|
||||
|
||||
# 使用selenium截图保存为PNG
|
||||
options = webdriver.ChromeOptions()
|
||||
options.add_argument('--headless')
|
||||
options.add_argument('--disable-gpu')
|
||||
options.add_argument('--window-size=1200,900')
|
||||
|
||||
driver = webdriver.Chrome(options=options)
|
||||
driver.get(f'file:///{os.path.abspath(html_path)}')
|
||||
time.sleep(3) # 等待页面加载
|
||||
|
||||
driver.save_screenshot('knowledge_graph_json.png')
|
||||
driver.quit()
|
Loading…
Reference in new issue