add_book.ipynb¶
This notebook guides in the action of adding books to your database.
The notebook is divided in two parts:
In the first part we create a minimal database with a single book. We save it as
database.dbin the current folder.In the second part we load the database
database.dband we add the book contained in the filenewbook.xlsx(created usingmbp.create_excel_template).
import polars as pl
import bookhoarder as bh
%opts magic unavailable (pyparsing cannot be imported)
%compositor magic unavailable (pyparsing cannot be imported)
Create minimal database¶
book = bh.new_book(
{
"isbn": "9783866473256",
"author": "Karl Marx",
"title": "Das Kapital",
"year": "2009",
"publisher": "Anaconda",
"full_price": 7.95,
"min_price": 7.95,
"buecher": "https://www.buecher.de/artikel/buch/das-kapital/25646129/",
"osiander": "https://www.osiander.de/shop/home/artikeldetails/A1006759980",
}
)
df = pl.DataFrame(book, schema_overrides=bh.schema())
df
| isbn | author | title | year | publisher | language | format | full_price | min_price | adelphi | buecher | feltrinelli | hoepli | hugendubel | ibs | libcoop | libraccio | libuni | mondadori | osiander | rizzoli |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| str | str | str | str | str | str | str | f64 | f64 | str | str | str | str | str | str | str | str | str | str | str | str |
| "9783866473256" | "Karl Marx" | "Das Kapital" | "2009" | "Anaconda" | null | null | 7.95 | 7.95 | null | "https://www.buecher.de/artikel… | null | null | null | null | null | null | null | null | "https://www.osiander.de/shop/h… | null |
bh.write_database(
df, # dataframe
"books", # table name
url="sqlite:///database.db", # database url
if_table_exists="replace", # behavior if table already exists
)
Add new book to database¶
We load the new book from the file newbook.xlsx:
df_2 = bh.read_excel("newbook.xlsx", schema=bh.schema())
df_2
| isbn | author | title | year | publisher | language | format | full_price | min_price | adelphi | buecher | feltrinelli | hoepli | hugendubel | ibs | libcoop | libraccio | libuni | mondadori | osiander | rizzoli |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| str | str | str | str | str | str | str | f64 | f64 | str | str | str | str | str | str | str | str | str | str | str | str |
| "9781857988826" | "Ursula K. Le Guin" | "The Dispossessed" | "1999" | "Orion Publishing Co" | null | null | 13.0 | 13.0 | null | "https://www.buecher.de/artikel… | "https://www.lafeltrinelli.it/d… | null | null | "https://www.ibs.it/dispossesse… | null | null | null | null | "https://www.osiander.de/shop/h… | null |
Then, we simply write it to the database specifying the attribute 'append':
bh.write_database(
df_2,
table_name="books",
url="sqlite:///database.db",
if_table_exists="append",
)
Note: if we wanted to make sure to not add duplicates (with respect to the ISBN), we can specify a
modify_df_before_writingfunction as an argument towrite_database. For example, we can use the functionmbp.delete_known_books, or the following function:def modify_df(**kwargs): df_db = pl.read_database( query=f"SELECT * FROM {kwargs['table_name']}", connection=kwargs["engine"], ) return df_2.join( df_db, on="isbn", how="anti" )
We check now what has been saved into the database:
bh.read_database(
table_name="books",
url="sqlite:///database.db",
schema=bh.schema(),
)
| isbn | author | title | year | publisher | language | format | full_price | min_price | adelphi | buecher | feltrinelli | hoepli | hugendubel | ibs | libcoop | libraccio | libuni | mondadori | osiander | rizzoli |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| str | str | str | str | str | str | str | f64 | f64 | str | str | str | str | str | str | str | str | str | str | str | str |
| "9783866473256" | "Karl Marx" | "Das Kapital" | "2009" | "Anaconda" | null | null | 7.95 | 7.95 | null | "https://www.buecher.de/artikel… | null | null | null | null | null | null | null | null | "https://www.osiander.de/shop/h… | null |
| "9781857988826" | "Ursula K. Le Guin" | "The Dispossessed" | "1999" | "Orion Publishing Co" | null | null | 13.0 | 13.0 | null | "https://www.buecher.de/artikel… | "https://www.lafeltrinelli.it/d… | null | null | "https://www.ibs.it/dispossesse… | null | null | null | null | "https://www.osiander.de/shop/h… | null |