Skip to content

Commit 3d0825c

Browse files
committed
Added pyproject.toml.
1 parent 97a7442 commit 3d0825c

8 files changed

Lines changed: 84 additions & 70 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,5 @@ venv.bak/
108108
.idea
109109

110110
# Credentials
111-
creds
111+
creds
112+
creds/*

README.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Pandas SQLAlchemy Tutorial
22

33
![Python](https://img.shields.io/badge/Python-v^3.8-blue.svg?logo=python&longCache=true&logoColor=white&colorB=5e81ac&style=flat-square&colorA=4c566a)
4-
![Pandas](https://img.shields.io/badge/Pandas-v^1.0.0-blue.svg?logo=Google&longCache=true&logoColor=white&colorB=5e81ac&style=flat-square&colorA=4c566a)
5-
![SQLAlchemy](https://img.shields.io/badge/SQLAlchemy-^1.3.6-red.svg?longCache=true&style=flat-square&logo=scala&logoColor=white&colorA=4c566a&colorB=bf616a)
6-
![PyMySQL](https://img.shields.io/badge/Psycopg2--Binary-v2.7.7-red.svg?longCache=true&style=flat-square&logo=PostgreSQL&logoColor=white&colorA=4c566a&colorB=bf616a)
4+
![Pandas](https://img.shields.io/badge/Pandas-v^1.0.0-blue.svg?logo=python&longCache=true&logoColor=white&colorB=5e81ac&style=flat-square&colorA=4c566a)
5+
![SQLAlchemy](https://img.shields.io/badge/SQLAlchemy-^1.3.6-red.svg?longCache=true&style=flat-square&logo=python&logoColor=white&colorA=4c566a&colorB=bf616a)
6+
![PyMySQL](https://img.shields.io/badge/PyMySQL-v0.9.3-red.svg?longCache=true&style=flat-square&logo=mysql&logoColor=white&colorA=4c566a&colorB=bf616a)
77
![GitHub Last Commit](https://img.shields.io/github/last-commit/google/skia.svg?style=flat-square&colorA=4c566a&colorB=a3be8c)
88
[![GitHub Issues](https://img.shields.io/github/issues/hackersandslackers/pandas-sqlalchemy-tutorial.svg?style=flat-square&colorA=4c566a&colorB=ebcb8b)](https://github.com/hackersandslackers/pandas-sqlalchemy-tutorial/issues)
99
[![GitHub Stars](https://img.shields.io/github/stars/hackersandslackers/pandas-sqlalchemy-tutorial.svg?style=flat-square&colorB=ebcb8b&colorA=4c566a)](https://github.com/hackersandslackers/pandas-sqlalchemy-tutorial/stargazers)
@@ -13,26 +13,37 @@
1313

1414
Easily drop data into Pandas from a SQL database, or upload your DataFrames to a SQL table. Tutorial found here: https://hackersandslackers.com/connecting-pandas-to-a-sql-database-with-sqlalchemy/
1515

16-
## Getting Started
1716

18-
Installation is recommended with Pipenv:
17+
## Installation
18+
19+
**Installation via `requirements.txt`**:
1920

2021
```shell
2122
$ git clone https://github.com/hackersandslackers/pandas-sqlalchemy-tutorial.git
2223
$ cd pandas-sqlalchemy-tutorial
23-
$ pipenv shell
24-
$ pipenv update
24+
$ python3 -m venv myenv
25+
$ source myenv/bin/activate
26+
$ pip3 install -r requirements.txt
2527
$ python3 main.py
2628
```
2729

28-
Alternatively, try installing via `setup.py`:
30+
**Installation via [Pipenv](https://pipenv-fork.readthedocs.io/en/latest/)**:
2931

3032
```shell
3133
$ git clone https://github.com/hackersandslackers/pandas-sqlalchemy-tutorial.git
3234
$ cd pandas-sqlalchemy-tutorial
33-
$ python3 setup.py install
35+
$ pipenv shell
36+
$ pipenv update
3437
$ python3 main.py
3538
```
36-
-----
3739

38-
**Hackers and Slackers** tutorials are free of charge. If you found this tutorial helpful, a [small donation](https://www.buymeacoffee.com/hackersslackers) would be greatly appreciated to keep us in business. All proceeds go towards coffee, and all coffee goes towards more content.
40+
## Usage
41+
42+
Replace the values in **.env.example** with your values and rename this file to **.env**:
43+
44+
* `SQLALCHEMY_DATABASE_URI`:Connection URI of a SQL database.
45+
46+
*Remember to never commit secrets saved in .env files to Github.*
47+
------------------
48+
49+
**Hackers and Slackers** tutorials are free of charge. If you found this tutorial helpful, a [small donation](https://www.buymeacoffee.com/hackersslackers) would be greatly appreciated to keep us in business. All proceeds go towards coffee, and all coffee goes towards more content.

config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77

88

99
SQLALCHEMY_DATABASE_URI = environ.get('SQLALCHEMY_DATABASE_URI')
10-
SQLALCHEMY_ENGINE_OPTIONS = {'ssl': {'ca': './creds/ca-certificate.crt'}}
11-
CSV_FILE = 'data/nyc-jobs.csv'
10+
SQLALCHEMY_ENGINE_OPTIONS = {'ssl': {'ca': './creds/ca-certificate.crt'}} # Optional for SSL connections
11+
CSV_FILE = 'data/nyc_jobs.csv'
Lines changed: 43 additions & 43 deletions
Large diffs are not rendered by default.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tool.poetry]
22
name = "Pandas SQLAlchemy Tutorial"
33
version = "0.1.0"
4-
description = "Easily drop data into Pandas from a SQL database or upload DataFrames to a SQL table."
4+
description = "Load or insert data into a SQL database using Pandas DataFrames."
55
authors = ["Todd Birchard <toddbirchard@gmail.com>"]
66
maintainers = ["Todd Birchard <toddbirchard@gmail.com>"]
77
license = "MIT"

tutorial/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010

1111
def init_script():
12+
"""Demonstrate Pandas' SQLAlchemy integration."""
1213
jobs_df = load_csv_data(CSV_FILE)
1314
db = Database(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_ENGINE_OPTIONS)
14-
db.upload_dataframe_to_sql(jobs_df)
15+
db.upload_dataframe_to_sql(jobs_df, 'nyc_jobs')
1516
db.get_dataframe_from_sql('nyc_jobs')

tutorial/database.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66

77
class Database:
8+
"""Pandas database client."""
89

910
def __init__(self, db_uri, db_args):
1011
self.engine = create_engine(
@@ -13,12 +14,12 @@ def __init__(self, db_uri, db_args):
1314
echo=True
1415
)
1516

16-
def upload_dataframe_to_sql(self, local_df):
17+
def upload_dataframe_to_sql(self, csv_df, table_name):
1718
"""Upload data to database with proper dtypes."""
18-
local_df.to_sql(
19-
"nyc_jobs",
19+
csv_df.to_sql(
20+
table_name,
2021
self.engine,
21-
if_exists='append',
22+
if_exists='replace',
2223
index=False,
2324
chunksize=500,
2425
dtype={
@@ -36,13 +37,15 @@ def upload_dataframe_to_sql(self, local_df):
3637
"posting_updated": DateTime
3738
}
3839
)
40+
print(f'Loaded {len(csv_df)} rows into {table_name} table.')
3941

4042
def get_dataframe_from_sql(self, table_name):
4143
"""Create DataFrame form SQL table."""
42-
sql_DF = pd.read_sql_table(
44+
table_df = pd.read_sql_table(
4345
table_name,
44-
con=self.engine,
45-
parse_dates=['posting_date', 'posting_updated']
46+
con=self.engine
4647
)
47-
return sql_DF
48+
print(f'Loaded {len(table_df)} rows from {table_name}.')
49+
print(table_df.info())
50+
return table_df
4851

tutorial/read.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44

55
def load_csv_data(csv):
6-
"""Create DataFrame and clean column names."""
6+
"""Create DataFrame from local CSV."""
77
jobs_df = pd.read_csv(csv)
8-
new_columns = [column.replace(' ', '_').lower() for column in jobs_df]
9-
jobs_df.columns = new_columns
108
return jobs_df

0 commit comments

Comments
 (0)