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:
- Passed through arguments
- The
~/.mpx/configfile - ENV vars located in
.env - 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mpxpy-0.0.4.tar.gz.
File metadata
- Download URL: mpxpy-0.0.4.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d3a4c2b2b56774741411e782759dd45ed2fe29c968767051cb976b47792f15c
|
|
| MD5 |
a24b5c1c152dba6fd40b7daf30fbc375
|
|
| BLAKE2b-256 |
a13f9d241b60bb1e831da180518dce4000a49287d97915619ff1408545d8b50c
|
File details
Details for the file mpxpy-0.0.4-py3-none-any.whl.
File metadata
- Download URL: mpxpy-0.0.4-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea9107cf84dfa4d2335854bc7fe8752ea46f94755e4f48dd51d8516b3dea56ed
|
|
| MD5 |
e286d8b4a4f3f054d6794b36cc5c8f7a
|
|
| BLAKE2b-256 |
ad2abe28a6346f9926910b4ca35229bd457dcb54a19e2a00c07623e36bedf2d9
|