Metadata-Version: 2.5
Name: matprops-sdk
Version: 0.2.0
Summary: Define and run Matprops models locally.
Project-URL: Homepage, https://matprops.com
Project-URL: Repository, https://github.com/rileyhanus/matprops
Project-URL: Issues, https://github.com/rileyhanus/matprops/issues
Author: MatProps
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.11
Requires-Dist: pydantic<3,>=2
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Description-Content-Type: text/markdown

# Matprops SDK

Local toolkit for typed material models: validate the contract, emit JSON Schema, run on your machine, and package or publish a zip to sim-server.

Requires Python 3.11+.

```bash
python -m pip install matprops-sdk
matprops init my-sim
cd my-sim
matprops validate
matprops run --input example.input.json
matprops package            # dist/<name>.zip, upload it in the web UI
matprops publish            # or package + upload in one step (needs login)
```

From a clone of this repo, install an editable copy with tests and lint:

```bash
python -m pip install -e ".[dev]"
```

`init` writes `model.py`, `pyproject.toml`, `README.md`, and `example.input.json` (it will not overwrite existing files). Entrypoint defaults to `[tool.matprops] entrypoint = "model:run"`; override with `--entrypoint`.

## CLI login

`matprops login` opens the Matprops web app (or prints a URL) so you can sign in and approve the CLI. Temporary credentials are stored in `~/.config/matprops/credentials.json` (`chmod 600`).

```bash
matprops login
matprops whoami
matprops logout
```

Defaults are production: `https://matprops.com` and `https://api.matprops.com`. Override with `MATPROPS_APP_URL` / `MATPROPS_API_URL` or `--app-url` / `--api-url`. For the local stack use `http://localhost:3000` and `http://localhost:4000`; for the shared dev environment use `https://dev.matprops.com` and `https://api.dev.matprops.com`. Use `--no-browser` on a remote machine and open the printed URL locally.

## Model

Use Pydantic models and `@model`. Sibling modules are importable as long as filenames are valid Python (`my_logic.py`, not `my-logic.py`).

```python
from matprops_sdk import Field, InputModel, OutputModel, RunContext, model


class Inputs(InputModel):
    radius: float = Field(gt=0, units="cm")


class Outputs(OutputModel):
    volume: float = Field(units="cm^3")


@model(
    name="volume-of-sphere",
    display_name="Volume of a sphere",
    inputs=Inputs,
    outputs=Outputs,
)
def run(inputs: Inputs, context: RunContext) -> Outputs:
    path = context.artifact("trace.txt", "Calculation trace")
    path.write_text("...", encoding="utf-8")
    return Outputs(volume=...)
```

The kebab-case `name` is the compute identity. `display_name` (optional, up to 120 characters) is the title shown in the Matprops catalog; publishing again with a new value updates it. Optional runtime metadata (`cpu`, `memory`, `timeout`) is recorded in schema/manifest and not enforced locally.

### README

A `README.md` at the project root is published with the model and rendered as its documentation on Matprops. Use it to describe the model, its inputs and outputs, and link to references. GitHub-flavoured Markdown is supported, plus LaTeX math with `$...$` inline and `$$...$$` blocks:

```markdown
The volume follows from the radius $r$:

$$
V = \frac{4}{3} \pi r^3
$$

See [Wikipedia](https://en.wikipedia.org/wiki/Sphere#Volume).
```

Embed images that live in the project with a path relative to the project root:

```markdown
![Band structure near the gap](figures/band-structure.png)
```

Referenced images are uploaded with the model and shown inline on Matprops. The rules:

- Only Markdown image syntax counts (inline `![alt](path)` or reference-style `![alt][id]`); raw HTML, including `<img>`, is not rendered.
- The file must be inside the project and packaged (not under `dist/`, `.venv/`, `.git/`, caches, and so on). Absolute paths, `..` outside the project, and remote `https://` images are rejected.
- Supported types: png, jpg/jpeg, gif, webp, svg. Up to 5 MiB per image and 20 distinct images per README.

`validate`, `package` and `publish` check these rules locally and stop with an error naming the bad reference; the platform checks them again on upload. Image syntax inside code blocks or math is ignored.

The README and its images are stored per model, so every publish (CLI or web upload) replaces them with the copy in the zip.

`validate` checks load, decorator, signature, schema, and README images only — not physics and not `example.input.json`.

Walkthrough with split logic: [`examples/volume-of-sphere/`](examples/volume-of-sphere/).

## CLI

| Command | What it does |
| --- | --- |
| `matprops init [dir]` | Scaffold a project |
| `matprops validate` | Check the entrypoint contract |
| `matprops schema [--output-dir DIR]` | Print schemas, or write `input.schema.json` / `output.schema.json` |
| `matprops run --input …` | Run locally |
| `matprops login` | Sign in via the web app and store credentials |
| `matprops logout` | Revoke and delete stored credentials |
| `matprops whoami` | Show the current CLI identity |
| `matprops package [--output PATH]` | Validate and write an uploadable zip to `dist/<name>.zip` |
| `matprops publish` | Package the project and POST it to sim-server |

`--input` is a file path, or inline JSON if the trimmed value starts with `{` or `[`:

```bash
matprops run --input '{"var1": 1, "var2": 2}'
```

Each run writes a timestamped folder under `.run-results/` (UTC, millisecond stamp, e.g. `.run-results/2026-09-01T15-11-22-123Z/`) with `outputs.json`, `manifest.json`, and `artifacts/`. `--output-dir` changes that parent directory.

Add `.run-results/` to gitignore.

## Packaging and publishing

`package` validates the contract in your environment (where your `requirements.txt` dependencies are installed), then zips the project directory together with a generated `manifest.json` holding the model name, display name, entrypoint, input/output JSON Schema and runtime hints. A root `README.md`, if present, is included and becomes the function's documentation. It skips `.git`, virtualenvs, `.run-results`, `dist`, caches, `*.pyc`, `.DS_Store` and root-level `example.input*.json` files. The zip is written to `dist/<name>.zip` (or `--output`) and is byte-for-byte reproducible for identical code.

The platform only accepts zips that contain this manifest: it reads the contract from `manifest.json` instead of importing your code, so uploads are validated instantly regardless of which packages the model needs. Upload the zip in the web UI, or let `publish` do it for you. Either way the image build installs your `requirements.txt` and re-derives the contract to confirm the manifest.

`publish` runs the same packaging step and POSTs the bundle to `{MATPROPS_API_URL}/models/publish` (default `https://api.matprops.com`). Authenticate with `matprops login`, `MATPROPS_API_KEY`, or `--api-key`. Override the URL with `MATPROPS_API_URL` or `--url`.

Add `dist/` to gitignore.

## Releasing

PyPI project name is `matprops-sdk` (`matprops` is taken). Versions live in both `pyproject.toml` (`[project].version`) and `src/matprops_sdk/__init__.py` (`__version__`); bump them together.

Do **not** tag releases as `v1`, `v13`, or other `v*` integers — those tags are used by the production deploy workflow. SDK tags are `sdk-vX.Y.Z` only.

### One-time setup

1. Create a [pypi.org](https://pypi.org/account/register) account with 2FA (and optionally a separate [test.pypi.org](https://test.pypi.org) account).
2. In GitHub: **Settings → Environments** → create `pypi` (URL `https://pypi.org/project/matprops-sdk/`) and optionally `testpypi`. Required reviewers are recommended on `pypi`.
3. On PyPI, add a [pending trusted publisher](https://pypi.org/manage/account/publishing/):
   - Project name: `matprops-sdk`
   - Owner: `rileyhanus`
   - Repository: `matprops`
   - Workflow filename: `python-sdk-publish.yml`
   - Environment name: `pypi`
4. Repeat on TestPyPI with environment `testpypi` if you want a dry run.

A typo in the workflow name or environment is a silent 403 on first upload.

### Publish a version

1. Bump both version fields, merge to `main`.
2. From `python/matprops-sdk`: `pip install -e ".[dev]" build twine`, then `ruff check . && pytest`, `python -m build`, and `python -m twine check dist/*`.
3. Tag the merged main SHA and push:

   ```bash
   git tag -a sdk-v0.1.0 -m "matprops-sdk 0.1.0"
   git push origin sdk-v0.1.0
   ```

4. Watch **Actions → Publish Python SDK**. Approve the `pypi` environment if reviewers are required.

The first successful trusted-publisher run creates `https://pypi.org/project/matprops-sdk/` and uploads that version. PyPI versions are immutable; a bad upload means yank and ship the next version.

Optional TestPyPI dry run: **Actions → Publish Python SDK → Run workflow** with target `testpypi`, then:

```bash
python -m pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ matprops-sdk
```

A first upload can also be done from a laptop with an account-scoped API token (`python -m twine upload dist/*`; username `__token__`). Later releases should use the `sdk-v*` tag so CI and Trusted Publishing stay the path of record.
