GithubHelp home page GithubHelp logo

jhrmnn / knitj Goto Github PK

View Code? Open in Web Editor NEW
50.0 4.0 1.0 456 KB

Alternative front-end to Jupyter kernels

License: Mozilla Public License 2.0

Python 86.33% JavaScript 7.86% Liquid 5.64% Vim Script 0.17%

knitj's Introduction

Knitj

python pypi commits since last commit license code style

Knitj is an alternative front-end to Jupyter kernels. Inspired by knitr and R Markdown, Knitj renders a mix of markdown and source code into HTML by evaluating the code in a Jupyter kernel.

In addition to a one-off conversion, Knitj can serve the HTML document via HTTP and watch the source file for changes. When the source file is changed, Knitj reevaluates only the changed bits (defined by boundaries between markdown and source code), and pushes the updates into the HTML document via WebSocket.

Example

Either of the two following files renders into the same HTML document below with

knitj $SOURCE >$SOURCE.html
```python
#::hide
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
```

## Example

Let's plot

$$ f(x)=\frac{\sin x}x $$

```python
x = np.linspace(-20, 20, 200)
plt.plot(x, np.sin(x)/x)
```
# ::hide
import numpy as np
from matplotlib import pyplot as plt
# ::%matplotlib inline

# ::>
# ## Example
#
# Let's plot
#
# $$ f(x)=\frac{\sin x}x $$

x = np.linspace(-20, 20, 200)
plt.plot(x, np.sin(x)/x)

Alternatively, one can start the Knitj server, which starts watching the source file for changes and opens a browser window with the rendered and live-updated HTML document

$ knitj --server test.py
[22:19:14.718] INFO:knitj: Entered Knitj
[22:19:14.722] INFO:knitj.document: File change: 3/0 new cells, 0 dropped
[22:19:14.732] INFO:knitj.document: 2 code cells loaded from output
[22:19:14.732] INFO:knitj.kernel: Starting kernel...
[22:19:15.145] INFO:knitj.kernel: Kernel started
[22:19:15.160] INFO:knitj.knitj: Started web server on port 8081
[22:19:15.441] INFO:knitj.knitj: Started broadcasting to browsers
[22:19:15.462] INFO:knitj.source: Started watching file test.md for changes
[22:19:15.881] INFO:knitj.webserver: Browser connected: 4542074160
[22:19:41.477] INFO:knitj.document: File change: 1/3 new cells, 1 dropped
[22:19:41.683] INFO:knitj.document: 72fea2: Got an error
[22:19:41.698] INFO:knitj.document: 72fea2: Cell done
[22:19:41.716] INFO:knitj.document: 72fea2: Got an error execution reply
^C[22:19:46.179] INFO:knitj.webserver: Closing websockets
[22:19:46.180] INFO:knitj.webserver: Browser disconnected: 4542074160
[22:19:46.181] INFO:knitj.kernel: Kernel shut down
[22:19:46.186] INFO:knitj: Leaving Knitj

Installing

Install and update using Pip.

pip install -U knitj

The following dependencies are installed:

To use Knitj, you also need some Jupyter kernel on your system. If you don’t have one, you can get the IPython kernel with

pip install ipykernel

Usage

usage: knitj [-h] [-s] [-f FORMAT] [-o FILE] [-k KERNEL] [-b BROWSER] [-n]
             [FILE]

positional arguments:
  FILE                  input file

optional arguments:
  -h, --help            show this help message and exit
  -s, --server          run in server mode
  -f FORMAT, --format FORMAT
                        input format
  -o FILE, --output FILE
                        output HTML file
  -k KERNEL, --kernel KERNEL
                        Jupyter kernel to use
  -b BROWSER, --browser BROWSER
                        browser to open
  -n, --no-browser      do not open a browser

knitj's People

Contributors

dependabot[bot] avatar jhrmnn 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  avatar  avatar  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

Forkers

inease

knitj's Issues

If no browser is available, knitj fails even with --no-browser

I am running knitj on a remote machine, planning to connect to the knitj server in my local browser. I run knitj -s -n file.md and get the following:

Traceback (most recent call last):
  File "/home/jh/.local/bin/knitj", line 11, in <module>
    sys.exit(main())
  File "/home/jh/.local/lib/python3.7/site-packages/knitj/cli.py", line 57, in main
    args = parse_cli()
  File "/home/jh/.local/lib/python3.7/site-packages/knitj/cli.py", line 40, in parse_cli
    default=webbrowser.get(),
  File "/home/jh/anaconda3/lib/python3.7/webbrowser.py", line 65, in get
    raise Error("could not locate runnable browser")
webbrowser.Error: could not locate runnable browser

It appears that the cli.py file is calling webbrowser.get() regardless of the --no-browser arg.

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.