Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

from html to jupyter notebook

from bs4 import BeautifulSoup
import json
import urllib.request
url = 'http://nbviewer.jupyter.org/url/jakevdp.github.com/downloads/notebooks/XKCD_plots.ipynb'
response = urllib.request.urlopen(url)
#  for local html file
# response = open("/Users/note/jupyter/notebook.html")
text = response.read()

soup = BeautifulSoup(text, 'lxml')
# see some of the html
print(soup.div)
dictionary = {'nbformat': 4, 'nbformat_minor': 1, 'cells': [], 'metadata': {}}
for d in soup.findAll("div"):
    if 'class' in d.attrs.keys():
        for clas in d.attrs["class"]:
            if clas in ["text_cell_render", "input_area"]:
                # code cell
                if clas == "input_area":
                    cell = {}
                    cell['metadata'] = {}
                    cell['outputs'] = []
                    cell['source'] = [d.get_text()]
                    cell['execution_count'] = None
                    cell['cell_type'] = 'code'
                    dictionary['cells'].append(cell)

                else:
                    cell = {}
                    cell['metadata'] = {}

                    cell['source'] = [d.decode_contents()]
                    cell['cell_type'] = 'markdown'
                    dictionary['cells'].append(cell)
open('notebook.ipynb', 'w').write(json.dumps(dictionary))
Comment

how to import html file to jupyter notebook

from IPython.display import HTML
HTML(filename='myhtml.html')
Comment

PREVIOUS NEXT
Code Example
Python :: value list in django 
Python :: what does enumerate do in python 
Python :: flask orm update query 
Python :: ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_3_1:0", shape=(None, None, 71), dtype=float32) at layer "input_3". The following previous layers were accessed without issue: [] 
Python :: reading files in python 
Python :: explicitly free memory in Python code 
Python :: convert pandas data frame to latex file 
Python :: Check version of package poetry 
Python :: how to run python in the browser 
Python :: Accessing elements from a Python Nested Dictionary 
Python :: append and extend in python 
Python :: current date to midnight 
Python :: django delete instance 
Python :: typeerror: 
Python :: python file get text by regular expression 
Python :: seaborn stripplot min max 
Python :: keras.callbacks.History 
Python :: converting list of arrays with same size to single array python 
Python :: python raise filenotfounderror 
Python :: check if variable is none 
Python :: mean absolute error in machine learning formula 
Python :: python regex true false 
Python :: pca in python 
Python :: how to get parent model object based on child model filter in django 
Python :: split strings around given separator/delimiter 
Python :: truthy falsy python 
Python :: python program to calculate the average of numbers in a given list 
Python :: virtual environment python 
Python :: are logN and (lognN) same 
Python :: start index from 1 in python 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =