Code Monkey home page Code Monkey logo

math-api's Introduction

Math API logo

Math API • Render LaTeX and MathML formulas as SVG or PNG.


A REST API to do fancy things with formulas, like rendering LaTeX or MathML to SVG or PNG on the server side using MathJax for Node, while leveraging expensive computations on the client.

As a Serverless application

You can deploy this repository as a serverless application using an AWS CloudFormation Template to create an AWS API Gateway that invokes Lambda functions to serve requests.

Launch this stack on AWS

As a Docker image

You can pull and run a Docker container to deploy the API on your local machine, server, Kubernetes cluster, whatever!

To start the container (it will bind on http://localhost:3000/):

docker run --name math-api -d -p 3000:3000 chialab/math-api

Endpoints

GET /render

An endpoint to render LaTeX and MathML formulas to SVG or PNG.

Query parameters:

  • input (required): the format of math in input.
    Valid values: latex, mathml
  • inline (optional): when input is latex, optionally enable "inline" mode.
    Valid values: 0, 1
  • source (required): the math to be rendered.
    Valid values: string, depends on the input type
  • output (required): the output format.
    Valid values: mathml, png, svg
  • width, height (optional): when output is png, specify the dimensions of the image to return.
    Valid values: positive integers

Examples:

GET /render?input=latex&output=svg&source=x^2 HTTP/1.1
Accept: image/svg+xml
GET /render?input=latex&inline=1&output=png&source=x^2&width=512 HTTP/1.1
Accept: image/png

POST /render

An endpoint to render LaTeX and MathML formulas to SVG or PNG.

Request body (JSON):

  • input (required): the format of math in input.
    Valid values: latex, mathml
  • inline (optional): when input is latex, optionally enable "inline" mode.
    Valid values: boolean
  • source (required): the math to be rendered.
    Valid values: string, depends on the input type
  • output (required): the output format.
    Valid values: mathml, png, svg
  • width, height (optional): when output is png, specify the dimensions of the image to return.
    Valid values: positive integers

Examples:

POST /render
Accept: image/svg+xml
Content-Type: application/json

{
    "input": "latex",
    "output": "svg",
    "source": "e^{i \\pi} + 1 = 0"
}
POST /render
Accept: image/png
Content-Type: application/json

{
    "input": "latex",
    "inline": true,
    "output": "png",
    "source": "e^{i \\pi} + 1 = 0",
    "width": 512
}

Development

All the following instructions assume you have at least NodeJS and Yarn installed.

Start the application locally:

yarn start

Run unit tests:

yarn run test

Start a simulated AWS API Gateway (provided you have AWS SAM Local and Docker installed):

yarn run api-gateway

Validate CloudFormation template (provided you have AWS CLI installed)

make validate

Package CloudFormation template (provided you have AWS CLI and Docker installed)

make layers (this is needed only the first time, then when updating MathJax version) make package

Deploy CloudFormation template (provided you have AWS CLI and Docker installed)

make deploy make deploy ENVIRONMENT=Production


License

Math API is released under the MIT license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.