Skip to main content

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

Project description

Convex

The official Python client for Convex.

PyPI GitHub

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("messages:list")
>>> from pprint import pprint
>>> pprint(messages)
[{'_creationTime': 1668107495676.2854,
  '_id': '2sh2c7pn6nyvkexbdsfj66vd9h5q3hg',
  'author': 'Tom',
  'body': 'Have you tried Convex?'},
 {'_creationTime': 1668107497732.2295,
  '_id': '1f053fgh2tt2fc93mw3sn2x09h5bj08',
  'author': 'Sarah',
  'body': "Yeah, it's working pretty well for me."}]
>>> client.mutation("sendMessage", dict(author="Me", body="Hello!"))

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
null None None
bigint ConvexInt64 (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
object dict {a: "abc"} collections.abc.Mapping

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 ConvexInt64 instances.

Convex Errors

The Python client supports the ConvexError type to hold application errors that are propagated from your Convex functions. To learn about how to throw ConvexErrors see https://docs.convex.dev/functions/error-handling/application-errors.

On the Python client, ConvexErrors are Exceptions with a data field that contains some ConvexValue. Handling application errors from the Python client might look something like this:

import convex
client = convex.ConvexClient('https://happy-animal-123.convex.cloud')

try:
    client.mutation("messages:sendMessage", {body: "hi", author: "anjan"})
except convex.ConvexError as err:
    if isinstance(err.data, dict):
        if "code" in err.data and err.data["code"] == 1:
            # do something
        else:
            # do something else
    elif isinstance(err.data, str):
        print(err.data)
except Exception as err:
    # log internally

Pagination

Paginated queries are queries that accept pagination options as an argument and can be called repeatedly to produce additional "pages" of results.

For a paginated query like this:

import { query } from "./_generated/server";

export default query(async ({ db }, { paginationOpts }) => {
  return await db.query("messages").order("desc").paginate(paginationOpts);
});

and returning all results 5 at a time in Python looks like this:

import convex
client = convex.ConvexClient('https://happy-animal-123.convex.cloud')

done = False
cursor = None
data = []

while not done:
    result = client.query('listMessages', {"paginationOpts": {"numItems": 5, "cursor": cursor}})
    cursor = result['continueCursor']
    done = result["isDone"]
    data.extend(result['page'])
    print('got', len(result['page']), 'results')

print('collected', len(data), 'results')

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.6.0a1.tar.gz (134.5 kB view details)

Uploaded Source

Built Distributions

convex-0.6.0a1-cp39-abi3-win_amd64.whl (844.0 kB view details)

Uploaded CPython 3.9+ Windows x86-64

convex-0.6.0a1-cp39-abi3-win32.whl (835.3 kB view details)

Uploaded CPython 3.9+ Windows x86

convex-0.6.0a1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (24.4 MB view details)

Uploaded CPython 3.9+ manylinux: glibc 2.17+ x86-64

convex-0.6.0a1-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (26.7 MB view details)

Uploaded CPython 3.9+ manylinux: glibc 2.17+ s390x

convex-0.6.0a1-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (23.9 MB view details)

Uploaded CPython 3.9+ manylinux: glibc 2.17+ ppc64le

convex-0.6.0a1-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (23.7 MB view details)

Uploaded CPython 3.9+ manylinux: glibc 2.17+ i686

convex-0.6.0a1-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (18.3 MB view details)

Uploaded CPython 3.9+ manylinux: glibc 2.17+ ARMv7l

convex-0.6.0a1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (23.5 MB view details)

Uploaded CPython 3.9+ manylinux: glibc 2.17+ ARM64

convex-0.6.0a1-cp39-abi3-macosx_11_0_arm64.whl (1.2 MB view details)

Uploaded CPython 3.9+ macOS 11.0+ ARM64

convex-0.6.0a1-cp39-abi3-macosx_10_12_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.9+ macOS 10.12+ x86-64

File details

Details for the file convex-0.6.0a1.tar.gz.

File metadata

  • Download URL: convex-0.6.0a1.tar.gz
  • Upload date:
  • Size: 134.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.3.0

File hashes

Hashes for convex-0.6.0a1.tar.gz
Algorithm Hash digest
SHA256 25ce6117388061ae0ec9293aac9a597de0c03534a3b2cf4981288d0da44ea5c7
MD5 99fa10e34fc1542dc3e3b569a08099c4
BLAKE2b-256 f88d0c520b41a1e0a3995b4ff60e452aa961ce4165fdb52fa3d833c89490df88

See more details on using hashes here.

File details

Details for the file convex-0.6.0a1-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for convex-0.6.0a1-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f30076ab9285eb7eed32ae1af464f5dfb1557ff8f2095630a718d960e1d4ef58
MD5 e576d630715841abcf8e9e1e4500312e
BLAKE2b-256 0cafea3cb2b47187d8b4c34eb074bc3193dc95361c642cb47b2a87be4d743215

See more details on using hashes here.

File details

Details for the file convex-0.6.0a1-cp39-abi3-win32.whl.

File metadata

  • Download URL: convex-0.6.0a1-cp39-abi3-win32.whl
  • Upload date:
  • Size: 835.3 kB
  • Tags: CPython 3.9+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.3.0

File hashes

Hashes for convex-0.6.0a1-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 87f5c3a0413843e6d815aa6da880d6e8b043e652c14819330109fe7cf1313a49
MD5 a977fbdc264c004e46c3fce3e19dd7a1
BLAKE2b-256 1a6f5c421a55f4b59d2e309795392fe4d600a519a0f3d7828d12346508263ec4

See more details on using hashes here.

File details

Details for the file convex-0.6.0a1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for convex-0.6.0a1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7aa4da23468f53954bce0ccbd5b3d757cb1902aee46fe815ab1d4520a0464c90
MD5 557c9f3f5daf91b46e29ccbaf8f8a27a
BLAKE2b-256 6987fa83d68dc0c013ea55f3ebc3aeb1d7a93adc38316bf3101292229f03e333

See more details on using hashes here.

File details

Details for the file convex-0.6.0a1-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for convex-0.6.0a1-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 95ad54bc5094901b3dd6bf96163b8e3272d2f1ad78d02bd1f88db407d2db452d
MD5 7cd1dc6f3e32886d37bfb258e537cdda
BLAKE2b-256 3df550e9e4e5e63be6cc89876ed435cf2bf38d8a508f77e9c8f9b027aa542d80

See more details on using hashes here.

File details

Details for the file convex-0.6.0a1-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for convex-0.6.0a1-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 e361d76d84df8053779f462efcfac2f00665aecb18c89ac2b2db6cfb29afee08
MD5 ed98837ac9d5b5a7f0782a51b2a85352
BLAKE2b-256 ab7dd5fe243113e8d9be41306719e4cb31a9c76f4a8c523c0a9d82b196d176b7

See more details on using hashes here.

File details

Details for the file convex-0.6.0a1-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for convex-0.6.0a1-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 32aab7f64a587377b2305d793ec4f2b8f5ba4c4263c4328a5da6223871b86447
MD5 7e7e26aa77d9e22c33ac828c8ec22e0c
BLAKE2b-256 0e7278372171875a77b190e69e6894fc5bebf347550e65620a336d3a99ff329b

See more details on using hashes here.

File details

Details for the file convex-0.6.0a1-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for convex-0.6.0a1-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 fbbe394d1f37a43a6c6b676b802d89469afd3b913067ca3e1196a005a8a777ec
MD5 fb30e6a6404ec9d7325d69e9d701d055
BLAKE2b-256 413d6c55885a937e8da9030baef40c08c49f5e4b3ea7b60970bdaad603df2190

See more details on using hashes here.

File details

Details for the file convex-0.6.0a1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for convex-0.6.0a1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d1911e4757bc3d3446154f1d04b951d436b50adfa6bdd90d859e5deda77e1ed4
MD5 6c908641af760f584aad53b280f5f912
BLAKE2b-256 e2c5b4e97e08db5d8ed118f0663b873f6a6cba021f4562754f8167415bd7254d

See more details on using hashes here.

File details

Details for the file convex-0.6.0a1-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for convex-0.6.0a1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1b18a8c9c547f385fdbadd650d000b94907894858e3a6e37b6e92db89eef9c7b
MD5 b2a26757264257a0477b6ceccd937dce
BLAKE2b-256 5da392ed90d0868a9e16a94a7a87db8e1ac56cf2899514140701b9236785751a

See more details on using hashes here.

File details

Details for the file convex-0.6.0a1-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for convex-0.6.0a1-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a0a3e7e8ac5b63380a972637499e5574196fe4d7d95897bb66180f98b9caaf79
MD5 9d70950fceb12d694df0c3d52887fba7
BLAKE2b-256 c0a4b5336ee8cb2bcdc24c4c08702be03dad0ec48382a0a158f6965daef23043

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