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.5.tar.gz (16.8 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.5-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mpxpy-0.0.5.tar.gz
  • Upload date:
  • Size: 16.8 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.5.tar.gz
Algorithm Hash digest
SHA256 31354ca91acfec19096d6dd77af82403f9877cf64c45ef7c5f4265fc211c9b07
MD5 45424eb84b6866941fa0d16944d658d4
BLAKE2b-256 f606ce0e8c58a9fee454693708a062d2572b0d435f8806fcbecf7cc4fb63dc43

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mpxpy-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 18.1 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c03a935a96b5926156f899b6dae6a5a959667b94c308b133e6d493f9cfead4d7
MD5 79815ab16f80cc3cf6cf0a50695dccc9
BLAKE2b-256 76103b658172cf39b47a3ed7ef02a792f863027fc339eec20f33f9cd19daf202

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