! pip install flask_sqlalchemy
from kgforge.core import KnowledgeGraphForge
A configuration file is needed in order to create a KnowledgeGraphForge session. A configuration can be generated using the notebook 00-Initialization.ipynb.
forge = KnowledgeGraphForge("../../configurations/forge.yml")
from kgforge.core import Resource
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine("sqlite:///:memory:")
jane = Resource(name="Jane Doe", email="jane.doe@epfl.ch")
john = Resource(name="John Smith", email="john.smith@epfl.ch")
other = Resource(name="John Doe", email="john.doe@unige.ch")
persons = [jane, john, other]
Converting Resources to DataFrame
df = forge.as_dataframe(persons)
Persisting Resources in the SQL Database.
Note: Another way would be to have a Store implementation for the SQL Database.
df.to_sql("Persons", engine, index=False)
Getting the table as a DataFrame
pd.read_sql("SELECT * FROM Persons", engine)
name | ||
---|---|---|
0 | jane.doe@epfl.ch | Jane Doe |
1 | john.smith@epfl.ch | John Smith |
2 | john.doe@unige.ch | John Doe |
Filtering on the SQL Database to keep only persons with an EPFL email
df = pd.read_sql("SELECT * FROM Persons WHERE email LIKE '%@epfl.ch'", engine)
df
name | ||
---|---|---|
0 | jane.doe@epfl.ch | Jane Doe |
1 | john.smith@epfl.ch | John Smith |
Converting DataFrame to Resources
persons = forge.from_dataframe(df)
print(*persons, sep="\n")
{ email: jane.doe@epfl.ch name: Jane Doe } { email: john.smith@epfl.ch name: John Smith }