python-fmrest is a wrapper around the FileMaker Data API.
No need to worry about manually requesting access tokens, setting the right http headers, parsing responses, ...
Some things you may use the python-fmrest library for:
If you haven't worked with Python and Virtualenvs before:
brew install python3
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
)pip3 install virtualenv
If you have worked with Python and Virtualenvs before, or after executing the steps above:
virtualenv venv --python=`which python3`
source venv/bin/activate
pip install python-fmrest
Link to this notebook: https://github.com/davidhamann/python-fmrest/tree/master/examples/conf_dotfmp_2018.ipynb
fmrest
extended privilege
In [ ]:
import fmrest
In [ ]:
fmrest.__version__
In [ ]:
fms = fmrest.Server(
'https://dotfmp-demo.davidhamann.de',
user='admin',
password='admin',
database='planets',
layout='Planets',
# if you are testing without cert/domain you may need the parameter verify_ssl=False here.
)
This gives you a server instance which provides all further methods to interact with the Data API.
In [ ]:
fms
Obtain a token from FMS:
In [ ]:
fms.login()
List all records from the Planets table
In [ ]:
planets = fms.get_records()
for planet in planets:
print(f'{planet.id}, {planet.record_id}, {planet.name}')
In [ ]:
type(planets), planets
Look at (some of) the moons of Jupiter (list records of a portal)
In [ ]:
record = fms.get_record(5, portals=[{'name': 'moons', 'limit': 5}])
portal = record['portal_moons']
record, portal
Fetching a record always gives you a Record instance. The portal rows, however, are returned as a Foundset.
In [ ]:
for row in portal:
print(row['Moons::name'])
You can inspect what fields are available:
In [ ]:
record.keys()
In [ ]:
record.values()
In [ ]:
record.to_dict()
And access the value by attribute or key:
In [ ]:
record.name, record['atmosphere']
So far we have seen Server, Foundset, Record. These are the main classes you need to be aware of when working with the library.
In [ ]:
find_request = [{'name': 'Earth'}, {'name': 'Jupiter'}]
foundset = fms.find(query=find_request)
earth = foundset[0]
earth
In [ ]:
earth.name = 'Blue Dot'
earth
In [ ]:
fms.edit(earth)
Handle outdated record values:
In [ ]:
# change back
earth.name = 'Earth'
fms.edit(earth, validate_mod_id=False)
In [ ]:
pluto = fms.create_record({'name': 'Pluto', 'id': 9})
pluto
In [ ]:
fms.delete_record(pluto)
In [ ]:
fms.get_record(
1,
scripts={
'after': ['say_hello', 'dotfmp']
}
)
fms.last_script_result
In [ ]:
fms.last_script_result['after'][1]
In [ ]:
with open('../scratch/dotfmp_logo.png', 'rb') as image:
result = fms.upload_container(3, 'image', image) # upload dotfmp logo into field with name "image" of record 3
result
Now retrieve the image again:
In [ ]:
earth = fms.get_record(3)
earth.image
In [ ]:
name, type_, length, response = fms.fetch_file(earth.image)
name, type_, length
In [ ]:
from IPython.display import Image
Image(response.content)
In [ ]:
find_request = [{'name': 'something that doesn\'t exist'}]
foundset = fms.find(query=find_request)
Turn Foundset into a Pandas DataFrame to do statistical analyses on your dataset, work with missing data, reshape/pivot, perform joins/merges, plot with matplotlib, export, etc.
In [ ]:
foundset = fms.get_records()
df = foundset.to_df()
df.loc[:, df.columns != 'image']
In [ ]:
df[['name', 'atmosphere', 'rings', 'confirmed_moons', 'mass']].set_index('name').T
In [ ]:
df.describe()
... or plot some data with matplotlib
In [ ]:
%matplotlib notebook
df.plot(x='name', y='confirmed_moons')
... or export the data in a different format
In [ ]:
path = 'data.csv'
df.to_csv(path, sep=";", index=False)
from IPython.display import FileLink
FileLink(path)
Read about Pandas here: https://pandas.pydata.org