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.0a2.tar.gz (186.9 kB view details)

Uploaded Source

Built Distributions

convex-0.6.0a2-cp39-abi3-win_amd64.whl (845.6 kB view details)

Uploaded CPython 3.9+ Windows x86-64

convex-0.6.0a2-cp39-abi3-win32.whl (834.2 kB view details)

Uploaded CPython 3.9+ Windows x86

convex-0.6.0a2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (27.8 MB view details)

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

convex-0.6.0a2-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (29.5 MB view details)

Uploaded CPython 3.9+ manylinux: glibc 2.17+ s390x

convex-0.6.0a2-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (26.6 MB view details)

Uploaded CPython 3.9+ manylinux: glibc 2.17+ ppc64le

convex-0.6.0a2-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (26.9 MB view details)

Uploaded CPython 3.9+ manylinux: glibc 2.17+ i686

convex-0.6.0a2-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (20.8 MB view details)

Uploaded CPython 3.9+ manylinux: glibc 2.17+ ARMv7l

convex-0.6.0a2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (27.0 MB view details)

Uploaded CPython 3.9+ manylinux: glibc 2.17+ ARM64

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

Uploaded CPython 3.9+ macOS 11.0+ ARM64

convex-0.6.0a2-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.0a2.tar.gz.

File metadata

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

File hashes

Hashes for convex-0.6.0a2.tar.gz
Algorithm Hash digest
SHA256 926772601a223958a92cdd9b8a4829b61bd5d1b13db36bca01ad1d03f2819f0b
MD5 aece16adeb742cd4d8943b3b555fc19f
BLAKE2b-256 31be56622945689254d17636555d18c5cdb169056bad751d57e6eeecf8677faa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for convex-0.6.0a2-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 497f409a8d00c77124b2d0f3c6ba233cecf73782668d7691da534feef62e6dc7
MD5 3ed0ce78c4af91e21c587ee8e412639c
BLAKE2b-256 d5df14e516a96664f2d4cd4efedb50ae2edf868862111f42a095872a27817549

See more details on using hashes here.

File details

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

File metadata

  • Download URL: convex-0.6.0a2-cp39-abi3-win32.whl
  • Upload date:
  • Size: 834.2 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.0a2-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 55c948242ec080b41525dda4e2579013a383978aaf5aa4e98574e68dea6e1ce0
MD5 83cc22c1e1006cd5e427a0839ef4c246
BLAKE2b-256 3e416f2673f79297d13b7e9ad3cfc845865399bb0b52e7d5b6cf12a43997e50a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for convex-0.6.0a2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d722e1f5a862cca38872e312d82840a1b48a1d9657d4d46a3d6de1554d093f93
MD5 c5b591263f740ac3b7689ad063a3c705
BLAKE2b-256 80e36a9c0c83f5cc861caab11a4df9fe9a110aa52f650b7157b59c532cfd8850

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for convex-0.6.0a2-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 372d3f395132b99dce8923e8f1c12b3e063c72c12c39e8b2c8aa2f13545de905
MD5 ffd2118da4c6651e15d5670fd19061af
BLAKE2b-256 8517ee0ddd7333720bf0ce78c11a207fa355ba709c22a5b90f6bf217cfa12784

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for convex-0.6.0a2-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 2a2e1efd1b72ad893418d6de9f320799a8bf95a0f438758b148f5e8a3301e079
MD5 f798c4432cae42d262fc1f2d750fe638
BLAKE2b-256 ebd97005d4b053ac6f2b1fb3891c42470e29ad7576187b3194a2b99048ea60f2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for convex-0.6.0a2-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 9f19b8ecde01f13736889d8ba17e3afc7f81b1e1617ecc58c4251ed9e2fbde1a
MD5 038674478ffa9ac55e4d4fd8432506cb
BLAKE2b-256 0e89dc68bde89635b083c1e09cfc1f530b97590265c0664fe6e4ed21d958d765

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for convex-0.6.0a2-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 2fbf2ccf94932b061f6fb1121884393bfc88d6e503e9b96a7d0810e93ee639db
MD5 780c9e5da317fc2884a28a43e9e47723
BLAKE2b-256 915030463d7ef5b0dd3b2d6e3f469982c704255535ea9a7306bcb3b7141ee596

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for convex-0.6.0a2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3de1636cfcd059ed941b71d2ed991220b34c88b726cf3f93b8311cbc70ed5714
MD5 c184470b9c8483cc9283a0214fa24757
BLAKE2b-256 65d7898e43e79e0975166c2138975684021274250e84977e5ebaabbd3f24a20b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for convex-0.6.0a2-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 814dc6da5397d2b1a414019d32ec45283386a852fd5bd1c30885212d9966fb8b
MD5 f6e3be34230ad5d3cbd54ecc67e9154c
BLAKE2b-256 6fb9ad28747081249f53fe057b4b84854ecc7bb9cd4b74ca64ebf23773f46100

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for convex-0.6.0a2-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9dde45883738909213c6132a247f711a10b325452558e0bbdd22ca8aa0a963a2
MD5 bf3be17664841b4ef738f515b96c9f90
BLAKE2b-256 44df4b562f38810bd7a41e7184aa78ce86b920ca62be0ba61234e271566b4b84

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