Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
How to connect to TerminusDB and TerminusCMS using the Python Client
First, you should install the Python Client. For instructions on installing see the installing python client how-to.
Depending on whether you are connecting to an instance you have set up yourself, or whether you are using TerminusCMS in the cloud, there are two different methods of connection.
In both cases, you should load TerminusDB in your script with the following:
The TerminusCMS endpoint has the form https://cloud.terminusdb.com/TEAM/
where TEAM
is the name of the team you are using in TerminusCMS for the data products you want to access.
In order to connect to this team, you will need to get your API key after selecting the team you want to use.
You should put your access token in your environment, using the environment variable TERMINUSDB_ACCESS_TOKEN
. This ensures that scripts do not leak the access token when checked into source control.
At this point you can create the client with this code:
Whether you are connecting to a local docker, a local server, or on a server that you've set up somewhere, you can use the following to log in to TerminusDB.
If you are using TerminusDB locally, and you have not set up a specific team, or changed the password from the default, you can simply connect with:
How-to delete a document in TerminusDB and TerminusCMS using the Python Client
In order to delete a document you need to know the document id.
How to connect to an existing database using the TerminusDB Python Client
If you have created a Team in TerminusCMS and put an API key in your environment you can connect to an existing database in the following way:
You can connect to a database with basic authorisation just by using the connect
member function.
If you want to connect as a specific user and with a specific password, you can pass them here:
How to install the TerminusDB Python Client to work with TerminusDB and TerminusCMS in Python
It is recommended that you install the TerminusDB Python client (which works with Python >= 3.7) in a separate Python environment. In the example, we use venv
which comes with the standard installation of Python 3. First, we create the new environment:
Then we can install using pip:
How-to create a database using the TerminusDB Python Client
To create a database with an already connected client, you can write:
This creates a new database called "MyDatabase"
using the descriptive label "My Database"
. It starts the database with special @base
and @schema
prefixes, all in the team named "MyTeam"
Add a schema to TerminusDB and TerminusCMS using the TerminusDB Python Client
After you have imported the terminusdb_client
, and created a client, and connected to a database you can create a schema.
You can update the schema by adding well-formed JSON schema documents:
How-to add documents to TerminusDB and TerminusCMS using the Python Client
After you have imported the terminusdb_client
, and created a client, connected to a database, and added some schema, you can then use this client to insert a document that conforms to the schema.
To insert a document, you should use insert_document
:
To insert multiple documents you can also invoke insert_document
:
Additionally, you can update the schema itself by adding schema documents:
How-to get documents to TerminusDB and TerminusCMS using the Python Client.
This guide assumes that you are already connected to the database using the Python client.
To get a single document to make changes or simply to view it, use the following code:
To get a list of all documents in the database, you can use the get_all_documents
function.
How to use the Python Client to import data in a CSV
This how-to assumes that you are already connected to a database and have a schema that matches the CSV you want to import.
You can import CSV files easily by importing them into dictionaries using Python's built-in libraries. Those dictionary objects can be inserted into the database using the insert_document
function.
How-to update a document in TerminusDB and TerminusCMS using the Python Client
To update a document in your database, you first need to get the document you want to change. You then need to make your changes and update it. This example shows how -
This how-to guide provides an example of the WOQL query language using the Python client.
Assume that you have installed the client, connected to a database, and connected with a client.
Writing WOQL queries in Python is fairly simple. We have a WOQLQuery class that can be used to construct WOQL Queries.
A simple example, in which we get all the names of the people in the database: