Coder Social home page Coder Social logo

jirutka / ipynb2html Goto Github PK

View Code? Open in Web Editor NEW
24.0 4.0 3.0 1.95 MB

Convert Jupyter (IPython) Notebooks to static HTML

Home Page: https://ipynb.js.org

License: MIT License

JavaScript 19.63% TypeScript 77.20% Shell 0.64% CSS 2.52%
jupyter ipynb-notebook notebook converter html

ipynb2html's Introduction

Jupyter Notebook to HTML

CI Status

ipynb2html is a converter (renderer) of the Jupyter Notebook Format 4.0+ to static HTML. It works both in Node.js and browser environment.

Packages

This repository contains the following packages, all published on npm.

ipynb2html-core

Version on npm Minified bundle size

This package provides the converter itself and some utilities with no dependencies. You have to provide your own syntax highlighter and Markdown, math and ANSI sequences renderer; or not, if you don’t need them.

ipynb2html

Version on npm Minified bundle size

This package builds on the ipynb2html-core and provides a complete, ready-to-go renderer configured with:

It also provides a reference stylesheet which you can find in dist/notebook.min.css (or non-minified styles/notebook.css).

ipynb2html-cli

Version on npm Minified bundle size

This package provides a CLI interface for ipynb2html.

Installation

All the packages can be installed using npm or yarn from npmjs.com.

Standalone CLI Tool

ipynb2html-cli is also provided as a single minified JavaScript with all the external dependencies bundled in. It requires only Node.js (version 10 or newer) to be installed on the system.

The archive also contains source maps (useful for debugging).

If you use Alpine Linux, you can also install it from package ipynb2html.

Usage

CLI

ipynb2html notebook.ipynb notebook.html

Run ipynb2html --help for more information.

Node.js (server-side)

To render HTML in Node.js (server-side rendering), you need some (fake) DOM implementation. The recommended one is nodom — it’s lightweight, small, doesn’t have any external dependencies and ipynb2html is tested against it. However, you can choose any other if you like.

npm install ipynb2html nodom
import * as fs from 'fs'
import * as ipynb from 'ipynb2html'
import { Document } from 'nodom'

const renderNotebook = ipynb.createRenderer(new Document())

const notebook = JSON.parse(fs.readFileSync('./example.ipynb', 'utf8'))

console.log(renderNotebook(notebook).outerHTML)

Browser (client-side)

You have basically two options how to use ipynb2html in the browser: use the browser bundles provided in the ipynb2html package, or build your own bundle (using e.g. Rollup or webpack).

The provided bundles are in UMD format (AMD, CommonJS and IIFE in one file), so they should work in all environments (old and modern browsers, Node.js). They are transpiled and have injected core-js polyfills to be compatible with browsers that have >0.5% global coverage, Firefox ESR, and not dead browsers.

Full Bundle

ipynb2html-full.min.js is a self-contained bundle with all the external dependencies included (marked, KaTeX, Anser and Highlight.js).

You can link it from jsDelivr CDN, for example:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/notebook.min.css" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/default.min.css" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ipynb2html-full.min.js" crossorigin="anonymous"></script>
  </head>
  ...
</html>

The bundle exposes global variable ipynb2html:

const element = ipynb2html.render(notebook)
document.body.appendChild(element)

ipynb2html also provides function autoRender that renders each notebook on the page embedded (as JSON) inside <script type="application/x-ipynb+json">...</script>.[1]

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/notebook.min.css" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/default.min.css" crossorigin="anonymous">
    <script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ipynb2html-full.min.js" crossorigin="anonymous"
        onload="ipynb2html.autoRender();"></script>
  </head>
  <body>
    <main>
      <script type="application/x-ipynb+json">
        {
          "cells": [ ... ],
          "metadata": { ... },
          "nbformat": 4,
          "nbformat_minor": 3
        }
      </script>
    </main>
  </body>
<html>

Slim Bundle

ipynb2html.min.js contains only ipynb2html and ipynb2html-core code (plus polyfills). If you load marked, KaTeX, AnsiUp, and Highlight.js in the page, you will get the same functionality as with ipynb2html-full.min.js:

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/default.min.css" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/marked.min.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/ansi_up.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ipynb2html.min.js" crossorigin="anonymous"></script>
  </head>
  ...
</html>

Or you may use any other implementations and provide them to the ipynb2html.createRenderer function. All of them are optional, but you usually need at least a Markdown renderer.

Development

System Requirements

Used Tools

  • TypeScript the language

  • ttypescript wrapper for tsc allowing to use custom AST transformers

  • yarn for dependencies management and building

  • ESLint for linting JS/TypeScript code

  • Jest for testing

  • Rollup for building single-file bundles

How to Start

  1. Clone this repository:

    git clone https://github.com/jirutka/ipynb2html.git
  2. Install Yarn (if you don’t have it already):

    npm install -g yarn
  3. Install all JS dependencies:

    yarn install
  4. Build the project:

    yarn build
  5. Run tests and generate code coverage:

    yarn test
  6. Run linter:

    yarn lint
Important
Keep in mind that JS sources are located in the src directories; lib directories contains transpiled code (created after running yarn build)!

Visual Studio Code

If you use Visual Studio Code, you should install the following extensions:

Credits

License

This project is licensed under MIT License. For the full text of the license, see the LICENSE file.


1. Don’t forget to escape HTML special characters: <, >, and &.

ipynb2html's People

Contributors

dependabot[bot] avatar jirutka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ipynb2html's Issues

XSS

when the 'cell_type' in 'cells' is markdown(data returned by the ipynb file), and 'source' contains <iframe src=\"javascript:alert(document.cookie)\"></iframe> , there will be XSS injection.

Formulas

The formula is rendered incorrectly, for example, "$A_{n}=A(1+\beta)^n-X\frac{(1+\beta)^n - 1}{\beta}$\n".

ipynb2html: command not found

Hi,

I'm trying to use your library to convert some of jupyter notebooks to html for my blog. I installed you dependency via npm but I get this error "ipynb2html: command not found" when I try to run the cli tool.

Any ideas why this is happening?

Can I change css for html?

Hi @jirutka thank you for sharing a nice package.
I want to customize css after convert to html(dark style). how can I customize css using highlight.js or other ways ?
thanks

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.