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 by either:

  • Using environment variables
  • Passing them directly when initializing the client

MathpixClient will prioritize auth configs in the following order:

  1. Passed through arguments
  2. The ~/.mpx/config file
  3. ENV vars located in .env
  4. ENV vars located in local.env

Using environment variables

Create a config file at ~/.mpx/config or add ENV variables to .env or local.env files:

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

# Will use ~/.mpx/config or environment variables
client = MathpixClient()

# Will use passed arguments
client = MathpixClient(
    app_id="your-app-id",
    app_key="your-app-key"
)

Features

Process a PDF

from mpxpy.mathpix_client import MathpixClient

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

# 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

from mpxpy.mathpix_client import MathpixClient

client = MathpixClient(
    app_id="your-app-id",
    app_key="your-app-key"
)
# 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()
print(lines)

Convert Mathpix Markdown (MMD)

from mpxpy.mathpix_client import MathpixClient

client = MathpixClient(
    app_id="your-app-id",
    app_key="your-app-key"
)
# Convert Mathpix Markdown to various formats
conversion = client.conversion_new(
    mmd="\\frac{1}{2}",
    conversion_formats={"docx": True}
)

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

# Download the converted output
docx_output = conversion.download_output("docx")

Error Handling

The client provides detailed error information in the following classes:

  • MathpixClientError
  • AuthenticationError
  • ValidationError
  • FilesystemError
  • ConversionIncompleteError
from mpxpy.mathpix_client import MathpixClient
from mpxpy.errors import MathpixClientError, ConversionIncompleteError

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

try:
    pdf = client.pdf_new(file_path="example.pdf", conversion_formats={'docx': True})
except FileNotFoundError as e:
    print(f"File not found: {e}")
except MathpixClientError as e:
    print(f"File upload error: {e}")
try:
    pdf.download_output_to_local_path('docx', 'output/path')
except ConversionIncompleteError as e:
    print(f'Conversions are not complete')

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.3.tar.gz (16.7 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.3-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mpxpy-0.0.3.tar.gz
  • Upload date:
  • Size: 16.7 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.3.tar.gz
Algorithm Hash digest
SHA256 eb8fe82f773f1251a5a75a5868f9eb0f60761346516815daece80ed8133f480f
MD5 48ce66a73931ab768d91fc1e12ae7a21
BLAKE2b-256 383f80f006d30a5200631b4eb0e8115c02ff752533d6d120e9609981a1e7fe82

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mpxpy-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 18.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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 da203fc39830125418e3a34c244b5aed23979511f2f736060012cfc5815f8e38
MD5 9a345103e4646796365cea06192e388a
BLAKE2b-256 6293558f4aa4f367510e7f2c0e498dce11b81e1c408383c886645f3ae36c74fc

See more details on using hashes here.

Release history Release notifications | RSS feed

0.0.22

2 files

0.0.21

2 files

0.0.20

2 files

0.0.19

2 files

0.0.18

2 files

0.0.17

2 files

0.0.16

2 files

0.0.15

2 files

0.0.14

2 files

0.0.13

2 files

0.0.12

2 files

0.0.10

2 files

0.0.9

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

This release

0.0.3 This release

2 files

0.0.2

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page