Skip to main content

The official Python library for the Ouro API

Project description

ouro-py

Version

The Ouro Python library provides convenient access to the Ouro REST API from any Python 3.7+ application. Visit Ouro to learn more about the Ouro platform.

Documentation

The REST API documentation can be found on ouro.foundation/docs/developers/api.

Installation

# install from PyPI
pip install ouro-py

Usage

Generate an API key from your account settings by going to ouro.foundation/settings/api-keys.

Set your Ouro environment variables in a dotenv file, or using the shell:

export OURO_API_KEY="your_api_key"

Init client:

import os
from ouro import Ouro

api_key = os.environ.get("OURO_API_KEY")
ouro = Ouro(api_key=api_key)

Use the client to interface with the Ouro framework.

Create a dataset

rows = [
    {"name": "Bob", "age": 30},
    {"name": "Alice", "age": 27},
    {"name": "Matt", "age": 26},
]

res = ouro.datasets.create(
    data=rows,  # also accepts a Pandas DataFrame or a single dict row
    name="your_dataset_name",
    description="your_dataset_description",
    visibility="private",
)

data is required for datasets.create(...) and must include at least one row and one column.

Read a dataset

id = "3d82308b-0747-45e4-8045-c8f7d2f6c0a6" # penguins dataset

# Retrieve a dataset
dataset = ouro.datasets.retrieve(id)

# Read dataset's data as a Pandas DataFrame
df = ouro.datasets.query(id)

Run SQL against a dataset

# Pass a SQL string as the 2nd arg to query(). Use {{table}} as the table
# placeholder; read-only enforced server-side, 10s statement timeout.
df = ouro.datasets.query(id, "SELECT species, count(*) AS n FROM {{table}} GROUP BY species")

Update a dataset

id = "3d82308b-0747-45e4-8045-c8f7d2f6c0a6"
data_update = [
    {"name": "Bob", "age": 30},
    {"name": "Alice", "age": 27},
    {"name": "Matt", "age": 26},
]

update = {
    "visibility": "private",
    "data": data_update,  # also accepts a DataFrame or single dict row
}
data = ouro.datasets.update("018f86da-b1be-7099-9556-fe88fb6882c3", **update)

Save a dataset view

view = ouro.datasets.create_view(
    "3d82308b-0747-45e4-8045-c8f7d2f6c0a6",
    name="Age Distribution",
    sql_query="select age, count(*) as total from {{table}} group by age order by age",
    config={
        "type": "bar",
        "xAxis": {"dataKey": "age", "type": "category"},
        "series": [{"dataKey": "total", "name": "People"}],
    },
)

views = ouro.datasets.list_views("3d82308b-0747-45e4-8045-c8f7d2f6c0a6")

Create a post

content = ouro.posts.Editor()
content.new_header(level=1, text="Hello World")
content.new_paragraph(text="This is a paragraph written in code.")

post = ouro.posts.create(
    content=content,
    name="Hello World",
    description="This is a post from the Python SDK",
    visibility="public",
)

You can also create a post from a local markdown file:

post = ouro.posts.create(
    name="Post From Markdown",
    content_path="/absolute/path/to/post.md",
    visibility="private",
)

Embed generated files in a post

Use partial assets to embed files that don't exist on the platform yet. The backend will materialise them into real file assets when the post is saved.

# Build a partial file payload from a local file
partial = ouro.files.partial_from_file(
    "/tmp/energy_curve.html",
    name="Energy curve",
    description="Energy vs. optimisation step",
    content_type="text/html",
)

# Or from raw bytes
partial = ouro.files.partial_from_bytes(
    html_bytes,
    "energy_curve.html",
    name="Energy curve",
    description="Energy vs. optimisation step",
)

# Embed it in a post
editor = ouro.posts.Editor()
editor.new_header(level=2, text="Results")
editor.new_partial_asset(partial, view_mode="preview")

post = ouro.posts.create(
    content=editor,
    name="Simulation Report",
    visibility="private",
)

partial_from_file and partial_from_bytes infer the MIME type and extension automatically when content_type is omitted. Pass an explicit id to new_partial_asset if you need a stable identifier for the embedded node.

Read a post

id = "b9ff1bfd-b3ae-4e92-9afc-70b1e1e2011a" # The post id

post = ouro.posts.retrieve(id)

Update a post

id = "b9ff1bfd-b3ae-4e92-9afc-70b1e1e2011a" # The post id

new_content = ouro.posts.Editor()
new_content.new_header(level=1, text="Hello World")
new_content.new_paragraph(text="This is a paragraph, but different this time.")

update = {
    "name": "Hello World",
    "visibility": "public",
    "content": new_content,
}
post = ouro.posts.update(id, **update)

Download any asset

result = ouro.assets.download(
    "3d82308b-0747-45e4-8045-c8f7d2f6c0a6",
    output_path="./downloads/",
)

print(result["path"])

assets.download(...) saves the asset to disk and returns the saved path, filename, content type, and byte count. Files download as their original bytes, datasets as .csv, and posts as .html.

Read the full API docs at ouro.foundation/docs/developers/api.

Contributing

Contributing to the Python library is a great way to get involved with the Ouro community. Reach out to us on our Github Discussions page if you want to get involved.

Set up a Local Development Environment

Clone the Repository

git clone git@github.com:ourofoundation/ouro-py.git
cd ouro-py

Create and Activate a Virtual Environment

We recommend activating your virtual environment. Click here for more about Python virtual environments and working with conda and poetry.

Using venv (Python 3 built-in):

python3 -m venv env
source env/bin/activate  # On Windows, use .\env\Scripts\activate

Using conda:

conda create --name ouro-py
conda activate ouro-py

PyPi installation

Install the package (for > Python 3.7):

# with pip
pip install ouro-py

Local installation

You can also install locally after cloning this repo. Install Development mode with pip install -e, which makes it so when you edit the source code the changes will be reflected in your python module.

Badges

License: MIT Python Last commit GitHub commit activity Github Stars Github Forks Github Watchers GitHub contributors

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

ouro_py-0.10.8.tar.gz (67.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ouro_py-0.10.8-py3-none-any.whl (86.7 kB view details)

Uploaded Python 3

File details

Details for the file ouro_py-0.10.8.tar.gz.

File metadata

  • Download URL: ouro_py-0.10.8.tar.gz
  • Upload date:
  • Size: 67.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for ouro_py-0.10.8.tar.gz
Algorithm Hash digest
SHA256 1bef96d15df2b4758f738e2939df473f17a8a4d50c02803e1899f8465a9954d2
MD5 7b78bbf74bc0ab1e14b812b2602e4982
BLAKE2b-256 b0946785de69fba78566fc6906b3d69cae22259285c8913df313251dabd442e4

See more details on using hashes here.

File details

Details for the file ouro_py-0.10.8-py3-none-any.whl.

File metadata

  • Download URL: ouro_py-0.10.8-py3-none-any.whl
  • Upload date:
  • Size: 86.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for ouro_py-0.10.8-py3-none-any.whl
Algorithm Hash digest
SHA256 e87ce6002a6c9aac32890e265ec8ecc457839bc6a857951e8fb994d0a5de7309
MD5 ba4b3e8326e7bfbe46ff692f6600028b
BLAKE2b-256 8753f468c2499d432e32b4cc9edd61bb224a918c307d34b0186bf21a791544e5

See more details on using hashes here.

Supported by

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