API tutorial 2 - Advanced OEP-API data queries using query parameters¶
Repository: https://github.com/openego/oedialect
Please report bugs and improvements here: https://github.com/OpenEnergyPlatform/examples/issues
How to get started with Jupyter Notebooks can be found here: https://realpython.com/jupyter-notebook-introduction/
Please ensure you have read the Terms of use here: https://openenergy-platform.org/legal/tou/
__copyright__ = "Reiner Lemoine Institut, Zentrum für nachhaltige Energiesysteme Flensburg"
__license__ = "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__ = "https://github.com/openego/data_processing/blob/master/LICENSE"
__author__ = "wolfbunke, Ludee"
Introduction¶
This tutorial gives you an overview of the OpenEnergy Platform and how you can work with the REST-full-HTTP API in Python.
The full API documentaion can be found on ReadtheDocs.io.
Part II - How to work with the OpenEnergy Platform (OEP)¶
0 Setup token
1 Select data from a table
2 Include filters
3 More Options
import requests
import pandas as pd
from IPython.core.display import HTML
from token_config import oep_url, get_oep_token
# token
your_token = get_oep_token()
1. Select data from a table¶
# select data
schema = 'model_draft'
table = 'example_api_table_test'
get_data = requests.get(oep_url + '/api/v0/schema/' + schema + '/tables/' + table + '/rows/')
get_data.status_code
Response [404] table doesn't exist!
# Convert to dataframe
plant_df = pd.DataFrame(get_data.json())
plant_df
# insert 'columnname=value'
where = 'type=wind_onshore'
result = requests.get(oep_url + '/api/v0/schema/' + schema + '/tables/' + table + '/rows/?where=' + where)
print(result.status_code)
# Convert to dataframe
df = pd.DataFrame(result.json())
df
Response [404] table doesn't exist!
2.2 column¶
# select columns by columnname
# select multiple columns using &
column = 'column=name&column=id&column=type'
result = requests.get(oep_url + '/api/v0/schema/' + schema + '/tables/' + table + '/rows/?' + column)
print(result.status_code)
# Convert to dataframe
df = pd.DataFrame(result.json())
df
Response [404] table doesn't exist!
2.3 limit¶
# set the number of results
limit = '2'
result = requests.get(oep_url + '/api/v0/schema/' + schema + '/tables/' + table + '/rows/?limit=' + limit)
print(result.status_code)
# Convert to dataframe
df = pd.DataFrame(result.json())
df
Response [404] table doesn't exist!
2.4 orderby¶
# insert column name
order_by = 'id'
result = requests.get(oep_url + '/api/v0/schema/' + schema + '/tables/' + table + '/rows/?orderby=' + order_by)
result.status_code
print(result)
# Convert to dataframe
df = pd.DataFrame(result.json())
df
Response [404] table doesn't exist!
# insert column name
order_by = 'id'
# set the number of results
limit = '2'
result = requests.get(oep_url + '/api/v0/schema/' + schema + '/tables/' + table + '/rows/?limit=' + limit + '&orderby=' + order_by)
print(result.status_code)
# Convert to dataframe
df = pd.DataFrame(result.json())
df
Response [404] table doesn't exist!
2.5 Combine filters¶
# insert 'columnname=value'
where = 'type=photovoltaics'
# select columns by columnname
# select multiple columns using &
column = 'column=name&column=id&column=type'
result = requests.get(oep_url + '/api/v0/schema/' + schema + '/tables/' + table + '/rows/?where=' + where + '&' + column)
print(result.status_code)
# Convert to dataframe
df = pd.DataFrame(result.json())
df
Response [404] table doesn't exist!
3. More Options¶
There are also optional parameters for these GET-queries:
offset: Ignore the specified amount of rows
where: Constraint fourmulated as VALUE+OPERATOR+VALUE with
1 VALUE: Constant or name of a column
2 OPERATOR: One of the following:
- EQUALS or
=
, - GREATER or
>
, - LOWER or
<
, - NOTEQUAL or
!=
or <>, - NOTGREATER or
<=
, - NOTLOWER or
>=