GithubHelp home page GithubHelp logo

alessandrosp / gspread_db Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 0.0 18 KB

A database-like API for gspread.

License: MIT License

Python 100.00%
gspread google-sheets google-sheets-api spreadsheets spreadsheet database

gspread_db's Introduction

gSpread DB

Anton Burnashev's gspread is a great package to interact with Google Spreadsheet in Python. The package offers a spreadsheet-like interface where users can create and remove worksheets, update cell values, append new values, etc. For many users that may be the most intuitive and convenient way to interact with Google Spreadsheet but for many applications Google Spreadsheet is fundamentally just another database and thus a database-like interface may make more sense.

gSpread DB adds a new API to gspread that supports the most common database operations like insert, delete, select and update. Specifically, a spreadsheet is considered a database, a worksheet a table and the first row of each table/worksheet the header of the table. All operations thus rely on this header to understand which fields is what.

You can install gspread_db via Pip:

pip3 install gspread_db

Then, you can start moving data around:

import gspread_db

# You can learn more about how to register your
# service and get API credentials at:
# https://gspread.readthedocs.io/en/latest/oauth2.html
spreadsheet_key = 'spreadsheet-key'
keyfile_dict = {
  'type': 'service_account',
  'project_id': 'you-project-id',
  'private_key_id': 'prive-key-id',
  'private_key': 'a-very-long-string',
  '...': '...'
}

scope = ['https://spreadsheets.google.com/feeds']
auth = oauth2client.service_account.ServiceAccountCredentials
credentials = auth.from_json_keyfile_dict(keyfile_dict, scope)

client = gspread_db.authorize(credentials)
db = client.open_by_key(spreadsheet_key)

db.create_table(table_name='Users', header=['Username', 'Email'])
users = db['Users']
users.insert({'Username': 'annoys_parrot', 'Email': '[email protected]'})
alessandro = users.select('Username', 'annoys_parrot')

Note that select operations return pd.DataFrame by default. This can be changed by setting the as_pandas argument to False.

It's important to note the header of the table (i.e. first row of the spreadsheet) is not just an aesthetic element. If we try to insert a new record with fields that are not contained in the header the operation will fail.

>>> _ = users.select(limit=1)
>>> users.header
['Username', 'Email']
>>> user.insert({'Password': '123456'})
RecordError: Keys in record must be a sub-set of header.

Note that as of version 1.0 in order for Table().header to return anything an operation must have been performed first (which is the reason why in the first line of the example above we select one row). This is because the header in the spreadsheet is parsed before every operation but not at instantiation time.

A more comprehensive example:

db.create_table(table_name='Users', header=['Username', 'Email'])
db['Users'].insert({'Username': 'Alan', 'Email': '[email protected]'})

# There are multiple ways to select users.
alan = db['Users'].select('Username', 'Alan', as_pandas=False)
turing = db['Users'].select(where=[('Username', 'eq', 'Alan')], as_pandas=False)

alan == turing  # True

print(alan)
# {
#   'Username': 'Alan',
#   'Email': '[email protected]'
# }

# We can also limit the number of fields returned.
alan_email = db['Users'].select('Username', 'Alan', fields=['Email'])

# We can update values for records matching some criteria.
db['Users'].update('Username', 'Alan', new_values={'Email': '[email protected]'})

# Lastly, we can delete records that match some criteria.
db['Users'].delete('Username', 'Alan')

# Note that for both update and delete all matching records will be updated
# or deleted. Make sure your conditions only match the right records!

Documentation

Note that because gspread_db is just a wrapper around gspread, all methods available in gspread for Spreasheet and Worksheet are also available in gspread_db for Database and Table.

gspread_db's People

Contributors

alessandrosp avatar bool- avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

gspread_db's Issues

How to insert a variable in the INSERT command?

Hey! I am a neophyte to programming, so, I am sorry if I am asking a stupid question but how to insert a variable in a particular row of a column?

Please provide a code example starting from selecting a spreadsheet by name method.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.