A simple way to view ipython notebook

2020-05-20 08:18发布

问题:

I am really new to IPython/Jupyter notebook. I just created one notebook (.ipynb) and I want to share it on my webpage. Specifically, I want to add a link, and when people click it, it will open a new "webpage" where they can "view" my code and results.

Note: I cannot use github, it is a huge pain for me.

I tried nbviewer (http://nbviewer.jupyter.org/). It has several options but only one of them (url) is not related to github/gist. So, in order to have an URL for my file, I uploaded it to google drive, and got a public link for the file. On the other hand, when I put that link to nbviewer (as url to my file), it says "there is no file in this url". On the other hand, I know the link works, because when I put it on browser, it directs me to download the .ipynb file.

I appreciate your help.

Thanks, J.

回答1:

There appears to be limited non-GitHub options for sharing notebooks. You can still share a link directly from Google's colaboratory. This will allow you to:

  • Upload your file
  • Share the link with various permissions

Any Google user can view (and optionally edit) your notebook.


See also other options:

  • binder: sharing notebooks from a GitHub repo; (see related blog post)
  • nbviewer: for viewing hosted notebooks from GitHub or a url (as mentioned)
  • JupyterHub: hosting notebooks on a private server, e.g. local, DigitalOcean, etc.
  • Azure Notebooks: host notebooks on an Azure server (see sample notebook)
  • repo2docker: spawn docker container from a git repo of notebooks
  • commuter: read notebooks from a local directory or S3 service
  • cocalc: collaborative and share private notebooks


回答2:

Checkout this Firefox plugin. Python Notebook Viewer

Its is easy to use, Does not require you to open terminal/command prompt and can be used offline as well. Just follow steps below.

  1. Install from Firefox Addons site
  2. Drag and drop .ipynb files into firefox.
  3. alternatively you can also open notebook from menu-> file -> open file

https://addons.mozilla.org/en-US/firefox/addon/python-notebook-viewer/



回答3:

As you already created a notebook file, you can easily convert it to an html file. In this format it will be easy for you to share it or put it on a website. So from the prompt :

jupyter nbconvert --to html --execute YOUR_FILE.ipynb --output OUTPUT.html

There is also other format : markdown, html, pdf, ipynb, etc

Documentation here



回答4:

If you want to share your Juptyer / IPython notebooks online, try using jovian.ml . It's a platform for sharing on collaborating on Jupyter notebooks, and it's really easy to use.

Step 1: Install the Jovian python library

pip install jovian

Step 2: Import the library inside your Jupyter / IPython notebook

import jovian

Step 3: Upload the notebook to your Jovian account by running

jovian.commit()

inside the Jupyter notebook. This will capture the Juptyer notebook (and also the Python libraries required to run it), and upload it your account, giving you shareable link. Here's an example: https://www.jovian.ml/aakashns/01-pytorch-basics

Viewers can also run your notebook on cloud platforms like Google Colab, BinderHub and Kaggle with a single click.



回答5:

A code below is a simple viewer for Jupyter notebooks. It can be used to preview quickly ipynb-files. Use the code as python jnv.py a.ipynb, where 'jnv.py' is the code below. The code can also be used in file managers, like Total Commander, if one assigns command python jnv.py as a viewer of ipynb-files.

# jnv.py: A simple viewer of a Jupyter notebooks (ipynb-files).
# Works for nbformat version >= 4.
import tkinter as tk
import sys,json

f = open(sys.argv[1], 'r',  encoding="utf8")  # input.ipynb
jf = json.load(f)
f.close()

# Take text ('source') from 'markdown' and 'code' cells
out_txt = ''
for cell in jf["cells"]:
   if cell['cell_type'] == 'markdown':
      for el in cell['source']:
         out_txt = out_txt + el
   elif  cell['cell_type'] == 'code':
      for el in cell['source']:
         out_txt = out_txt + el

# Make a frame and display 'out_txt'. Press Esc to quit.
# See https://www.python-course.eu/tkinter_text_widget.php
root = tk.Tk()
def key(event):
   if event.keycode == 27:  # pressed Esc 
      root.destroy()         

S = tk.Scrollbar(root)
T = tk.Text(root, height=24, width=80)
S.pack(side=tk.RIGHT, fill=tk.Y)
T.pack(side=tk.LEFT, fill=tk.Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)

T.insert(tk.END, out_txt)
root.bind("<Key>", key)
tk.mainloop()