# formatter None is used to preserve # the html signs rendered = soup. replace_with ( code ) print ( new_tag ) # create the final html string. rstrip (), lexer, html_formatter ) # replacing with formatted code new_tag = pre. 'output_text' in pclass ): continue # highlighting with pygments lexer = guess_lexer ( pre. find_all ( 'pre' ): # escaping pres in the jupyter notebook # either they're already formatted (input code), # or they should remain unformatted (ouput code) if pre. render () # replace the pre tags by highlighted code soup = BeautifulSoup ( rendered, 'html.parser' ) for pre in soup. get_template ( input_file ) # render the template # in other words, we replace the template tag # by the contents of the overfitting file rendered = template. Import os from jinja2 import Environment, FileSystemLoader from bs4 import BeautifulSoup from pygments import highlight from pygments.lexers import PythonLexer, guess_lexer from pygments.formatters import HtmlFormatter html_formatter = HtmlFormatter () # this tells jinja2 to look for templates # in the templates subdirectory env = Environment ( loader = FileSystemLoader ( 'templates' ), ) input_file = 'main.html' output_file = 'index.html' # reading the template template = env. The basic template mode of nbconvert is the one we're going to use. Also, the file contains opening and closing tags.īut the contents of such a file cannot be seamlessly inserted in a web page, nor in a full blown website.Ī solution could be to include this file in an iframe, but there are two important drawbacks: The style within the iframe is not going to be consistent with the style of the rest of the page, and you'll get a fixed iframe viewport with a vertical scrollbar that you are forced to use to read the whole jupyter notebook. This works because all the necessary CSS (style sheets) and javascript are included either in the HTML header or in the HTML body. Moreover, the code is highlighted, and the math equations written in markdown appear properly. You get a nice style for the input and output cells, for the tables, and for the plots. The resulting file, my_notebook.html, can be loaded in a web browser, and looks exactly like a live jupyter notebook. Here, I set up a static HTML page to be able to help you, whatever path you take.Īnd all the code is available in this github repository. By the way, if you want a static blog, you should definitely consider Jekyll. You could decide to go for another python-based web framework like Flask, a PHP-based framework, or even stay away from web frameworks and write plain static pages. I personally use django as a web framework, with wagtail as CMS. However, in the following, I will assume that you want to do better, and that you are writing your own HTML pages. You can try it if you like, and even come back here to ask questions in the comments if you need to. But that's in fact more difficult and the result is not as good. There are ways to do this with a content management system (CMS) like WordPress, and I was actually doing that before. apply an elegant overall style with Bootstrap,.integrate perfectly a jupyter notebook in a web page,.When you're done with this exercise, you will know how to: In this post, you'll learn how to set up a web page to communicate scientific results with style!
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |