Skip to main content

mpxpy

The official Python client for the Mathpix API. Process PDFs, images, and convert math/text content with the Mathpix API.

Installation

pip install mpxpy

Authentication

You'll need a Mathpix API app_id and app_key to use this client. You can get these from the Mathpix Console.

Set your credentials using either environment variables or pass them directly when initializing the client. MathpixClient will prioritize auth configs passed through arguments over ENV vars

Using environment variables

Create a local.env file:

MATHPIX_APP_ID=your-app-id
MATHPIX_APP_KEY=your-app-key
MATHPIX_URL=https://api.mathpix.com  # optional, defaults to this value

Passing credentials directly

from mpxpy.mathpix_client import MathpixClient

client = MathpixClient(
    app_id="your-app-id",
    app_key="your-app-key"
)

Then initialize the client:

from mpxpy.mathpix_client import MathpixClient

client = MathpixClient()  # Will use environment variables

Features

Process a PDF

from mpxpy.mathpix_client import MathpixClient

client = MathpixClient()

# Process a PDF file
pdf_file = client.pdf_new(
    file_url="http://cs229.stanford.edu/notes2020spring/cs229-notes1.pdf",
    conversion_formats={
        "docx": True,
        "md": True
    }
)

# Wait for processing to complete
pdf_file.wait_until_complete(timeout=60)

# Download the converted files
pdf_file.download_output_to_local_path("docx", "./output")
pdf_file.download_output_to_local_path("md", "./output")

Process an Image with Math Content

from mpxpy.mathpix_client import MathpixClient

client = MathpixClient()

# Process an image file
image = client.image_new(
    file_url="https://mathpix-ocr-examples.s3.amazonaws.com/cases_hw.jpg"
)

# Get the Mathpix Markdown (MMD) representation
mmd = image.mmd()
print(mmd)

# Get line-by-line OCR data
lines = image.lines_json()

Convert Mathpix Markdown (MMD)

from mpxpy.mathpix_client import MathpixClient

client = MathpixClient()

# Convert Mathpix Markdown to various formats
conversion = client.conversion_new(
    mmd="\\frac{1}{2}",
    formats={"latex": {}}
)

# Wait for conversion to complete
conversion.wait_until_complete(timeout=30)

# Download the converted output
latex_output = conversion.download_output("latex")

Error Handling

The client provides detailed error information:

from mpxpy.mathpix_client import MathpixClient, MathpixClientError

client = MathpixClient(app_id="your-app-id", app_key="your-app-key")

try:
    pdf_file = client.pdf_new(file_path="nonexistent.pdf")
except FileNotFoundError as e:
    print(f"File not found: {e}")
except MathpixClientError as e:
    print(f"API error: {e}")

Development

Setup

# Clone the repository
git clone git@github.com:Mathpix/mpxpy.git
cd mpxpy

# Install in development mode
pip install -e .

Running Tests

# Install test dependencies
pip install -e ".[dev]"

# Run tests
pytest

Download files

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

Source Distribution

mpxpy-0.0.1.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

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

mpxpy-0.0.1-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file mpxpy-0.0.1.tar.gz.

File metadata

  • Download URL: mpxpy-0.0.1.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.13

File hashes

Hashes for mpxpy-0.0.1.tar.gz
Algorithm Hash digest
SHA256 15893fa79acaa439792bdc4a0bf677c2f34cd5fca7e4577073cc01d4179ee5cd
MD5 36c7b83c48e5037aa32fff033fb877ba
BLAKE2b-256 7202c18792f09560ecb465287d98a6b41d2bab3785383d4ab5cf3e3efe1f6ee4

See more details on using hashes here.

File details

Details for the file mpxpy-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: mpxpy-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.13

File hashes

Hashes for mpxpy-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 de40ab2bc2a1926146768bd985c138087a5703186f3be492ee4568c25d205076
MD5 ef8307001687b0ae046f5dbaf226aec7
BLAKE2b-256 bfca78bf283fdbf340a24bc62b15eab18a90d970f1f71ac343256397a98fafba

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 Sentry Error logging StatusPage Status page