Skip to main content

Python client for the reactive backend-as-a-service Convex.

Project description

Convex

The official Python client for Convex.

Convex is the TypeScript-native programmable database for the web. No need for backend containers, caches, queues, and query languages. Convex replaces all of them with a few simple APIs.

This Python client can write and read data from a Convex backend with queries, mutations, and actions. Get up and running at docs.convex.dev.

Installation:

pip install convex

Basic usage:

>>> from convex import ConvexClient
>>> client = ConvexClient('https://example-lion-123.convex.cloud')
>>> messages = client.query("listMessages")
>>> from pprint import pprint
>>> pprint(messages)
[{'_creationTime': 1668107495676.2854,
  '_id': Id(table_name='messages', id='c09S884lW4kTLdQMtu2ravf'),
  'author': 'Tom',
  'body': 'Have you tried Convex?'},
 {'_creationTime': 1668107497732.2295,
  '_id': Id(table_name='messages', id='G3m0cCQp65GQDfUjUDnTPEj'),
  'author': 'Sarah',
  'body': "Yeah, it's working pretty well for me."}]
>>> client.mutation("sendMessage")

To find the url of your convex backend, open the deployment you want to work with in the appropriate project in the Convex dashboard and click "Settings" where the Deployment URL should be visible. To find out which queries, mutations, and actions are available check the Functions pane in the Dashboard

To see logs emitted from Convex functions, set the debug mode to True.

>>> client.set_debug(True)

To provide authentication for function execution, call set_auth().

>>> client.set_auth("token-from-authetication-flow")

Join us on Discord to get your questions answered or share what you're doing with Convex. If you're just getting started, see https://docs.convex.dev to see how to quickly spin up a backend that does everything you need in the Convex cloud.

Convex types

Convex backend functions are written in JavaScript, so arguments passed to Convex RPC functions in Python are serialized, sent over the network, and deserialized into JavaScript objects. To learn about Convex's supported types see https://docs.convex.dev/using/types.

In order to call a function that expects a JavaScript type, use the corresponding Python type or any other type that coerces to it. Values returned from Convex will be of the corresponding Python type.

JavaScript Type Python Type Example Other Python Types that Convert
Id Id (see below) Id(tableName, id)
null None None
bigint ConvexBigInt (see below) ConvexInt64(2**60)
number float or int 3.1, 10
boolean bool True, False
string str 'abc'
ArrayBuffer bytes b'abc' ArrayBuffer
Array list [1, 3.2, "abc"] tuple, collections.abc.Sequence
Set ConvexSet (see below) ConvexSet([1,2]) set, frozenset, collections.abc.Set
Map ConvexMap (see below) ConvexMap([('a', 1), ('b', 2)])
object dict {a: "abc"} collections.abc.Mapping

Id

Id objects represent references to Convex documents. They contain a table_name string specifying a Convex table (tables can be viewed in the dashboard) and a globably unique id string. If you'd like to learn more about the id string's format, see our docs.

Ints and Floats

While Convex supports storing Int64s and Float64s, idiomatic JavaScript pervasively uses the (floating point) Number type. In Python floats are often understood to contain the ints: the float type annotation is generally understood as Union[int, float].

Therefore, the Python Convex client converts Python's floats and ints to a Float64 in Convex.

To specify a JavaScript BigInt, use the ConvexInt64 class. Functions which return JavaScript BigInts will return ConvexBigInt64 instances.

ConvexSet

Similar to a Python set, but any Convex values can be items.

ConvexSets are returned from Convex cloud function calls that return JavaScript Sets.

Generally when calling Convex functions from Python, a Python builtin set can be used instead of a ConvexSet. But for representing unusual types like sets containing objects, you'll have to use a ConvexSet:

>>> set([{'a': 1}])
Traceback (most recent call last):
    ...
TypeError: unhashable type: 'dict'
>>> ConvexSet([{'a': 1}])
ConvexSet([{'a': 1.0}])

ConvexSet instances are immutable so must be fully populated when being constructed. In order to store mutable items, ConvexSets store snapshots of data when it was added.

>>> mutable_dict = {'a': 1}
>>> s = ConvexSet([mutable_dict, 'hello', 1])
>>> mutable_dict in s
True
>>> mutable_dict['b'] = 2
>>> mutable_dict in s
False
>>> s
ConvexSet([{'a': 1.0}, 'hello', 1.0])

ConvexSets perform a copy of each inserted item, so they require more memory than Python's builtin sets.

ConvexMap

Similar to a Python map, but any Convex values can be keys.

ConvexMaps are returned from Convex cloud function calls that return JavaScript Maps.

ConvexMaps are useful when calling Convex functions that expect a Map because dictionaries correspond to JavaScript objects, not Maps.

ConvexMap instances are immutable so must be fully populated when being constructed. In order to store mutable items, ConvexMaps store snapshots of data when it was added.

>>> mutable_dict = {'a': 1}
>>> s = ConvexMap([(mutable_dict, 123), ('b', 456)])
>>> mutable_dict in s
True
>>> mutable_dict['b'] = 2
>>> mutable_dict in s
False
>>> s
ConvexMap([({'a': 1.0}, 123.0), ('b', 456.0)])

ConvexMaps perform a copy of each inserted key/value pair, so they require more memory than Python's builtin dictionaries.

Versioning

While we are pre-1.0.0, we'll update the minor version for large changes, and the patch version for small bugfixes. We may make backwards incompatible changes to the python client's API, but we will limit those to minor version bumps.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

convex-0.2.0.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

convex-0.2.0-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file convex-0.2.0.tar.gz.

File metadata

  • Download URL: convex-0.2.0.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.9.6 Darwin/22.2.0

File hashes

Hashes for convex-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ac20565bf63bb5fb9661d256315d9ff0010d3bd1a263ecd2dd0d5b1de85cc9e0
MD5 1e6ca7b1314faf53056d0918df21bd09
BLAKE2b-256 a2959e7173fa7a0419ad8baabff20db8b3f899acc79018f79e5d11799ff2726b

See more details on using hashes here.

File details

Details for the file convex-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: convex-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.9.6 Darwin/22.2.0

File hashes

Hashes for convex-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b0b781594c5fac76333776dd14dd283695b659f46b28d2b2dd7245ab07eab011
MD5 f2a4854c0debd6e63dbef5de12a57981
BLAKE2b-256 afe0fd2cfb5a525add9219c555d3908d6f2bf147300a575d0b92b3d3a35d796c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page