def nb_renderer(full_path): directory, base = split(full_path) cache_file = join(directory, '.%s.html' % base) if not current_app.config.get('DEBUG'): try: if isfile(cache_file) and getmtime(full_path) < getmtime(cache_file): current_app.logger.debug('Using Cache File %s' % cache_file) return raw_renderer(cache_file) except: current_app.logger.warn('There was an error reading from the cache file %s' % cache_file) ex = HTMLExporter(extra_loaders=[current_app.jinja_env.loader], template_file='wakari_notebook.html') ex.environment.globals.update(current_app.jinja_env.globals) current_app.update_template_context(ex.environment.globals) ex.environment.globals.update(dirname=dirname(request.view_args['path'])) output, _ = ex.from_filename(full_path) try: with open(cache_file, 'w') as fd: current_app.logger.debug('Writing Cache File %s' % cache_file) fd.write(output.encode(errors='replace')) except (OSError, IOError): current_app.logger.warn('There was an error writing to the cache file %s' % cache_file) try: if isfile(cache_file): os.unlink(cache_file) except OSError: current_app.logger.warn('There was an error removing the cache file %s' % cache_file) pass return output
def read(self, filepath): metadata = {} # Files filedir = os.path.dirname(filepath) filename = os.path.basename(filepath) metadata_filename = filename.split('.')[0] + '.ipynb-meta' metadata_filepath = os.path.join(filedir, metadata_filename) # Load metadata if os.path.exists(metadata_filepath): # Metadata is on a external file, process using Pelican MD Reader md_reader = MarkdownReader(self.settings) _content, metadata = md_reader.read(metadata_filepath) else: # Load metadata from ipython notebook file ipynb_file = open(filepath) metadata = json.load(ipynb_file)['metadata'] # Fix metadata to pelican standards for key, value in metadata.items(): del metadata[key] key = key.lower() metadata[key] = self.process_metadata(key, value) metadata['ipython'] = True # Convert ipython notebook to html config = Config({'CSSHTMLHeaderTransformer': {'enabled': True, 'highlight_class': '.highlight-ipynb'}}) exporter = HTMLExporter(config=config, template_file='basic', filters={'highlight2html': custom_highlighter}) content, info = exporter.from_filename(filepath) # Process using Pelican HTMLReader content = '<body>{0}</body>'.format(content) # So Pelican HTMLReader works parser = MyHTMLParser(self.settings, filename) parser.feed(content) parser.close() body = parser.body summary = parser.summary metadata['summary'] = summary # Remove some CSS styles, so it doesn't break the themes. def filter_tags(style_text): style_list = style_text.split('\n') exclude = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a', 'ul', 'ol', 'li', '.rendered_html', '@media', '.navbar', 'nav.navbar', '.navbar-text', 'code', 'pre', 'div.text_cell_render'] style_list = [i for i in style_list if len(list(filter(i.startswith, exclude))) == 0] ans = '\n'.join(style_list) return '<style type=\"text/css\">{0}</style>'.format(ans) css = '\n'.join(filter_tags(css) for css in info['inlining']['css']) css = css + CUSTOM_CSS body = css + body return body, metadata
def read(self, filepath): metadata = {} # Files filedir = os.path.dirname(filepath) filename = os.path.basename(filepath) metadata_filename = filename.split('.')[0] + '.ipynb-meta' metadata_filepath = os.path.join(filedir, metadata_filename) # Load metadata if os.path.exists(metadata_filepath): # Metadata is on a external file, process using Pelican MD Reader md_reader = MarkdownReader(self.settings) _content, metadata = md_reader.read(metadata_filepath) else: # Load metadata from ipython notebook file ipynb_file = open(filepath) metadata = json.load(ipynb_file)['metadata'] # Fix metadata to pelican standars for key, value in metadata.items(): del metadata[key] key = key.lower() metadata[key] = self.process_metadata(key, value) metadata['ipython'] = True # Convert ipython notebook to html config = Config({'CSSHTMLHeaderTransformer': {'enabled': True, 'highlight_class': '.highlight-ipynb'}}) exporter = HTMLExporter(config=config, template_file='basic', filters={'highlight2html': custom_highlighter}) content, info = exporter.from_filename(filepath) # Process using Pelican HTMLReader content = '<body>{0}</body>'.format(content) # So Pelican HTMLReader works parser = MyHTMLParser(self.settings, filename) parser.feed(content) parser.close() body = parser.body summary = parser.summary metadata['summary'] = summary # Remove some CSS styles, so it doesn't break the themes. def filter_tags(style_text): style_list = style_text.split('\n') exclude = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a', 'ul', 'ol', 'li', '.rendered_html', '@media', '.navbar', 'nav.navbar', '.navbar-text', 'code', 'pre', 'div.text_cell_render'] style_list = [i for i in style_list if len(list(filter(i.startswith, exclude))) == 0] ans = '\n'.join(style_list) return '<style type=\"text/css\">{0}</style>'.format(ans) css = '\n'.join(filter_tags(css) for css in info['inlining']['css']) css = css + CUSTOM_CSS body = css + body return body, metadata
def nb2html(nb): """ Cribbed from nbviewer """ config = Config() config.HTMLExporter.template_file = 'basic' config.NbconvertApp.fileext = "html" config.CSSHtmlHeaderTransformer.enabled = False C = HTMLExporter(config=config) return C.from_notebook_node(nb)[0]
def compile_html_string(self, source, is_two_file=True): """Export notebooks as HTML strings.""" if flag is None: req_missing(['ipython[notebook]>=2.0.0'], 'build this site (compile ipynb)') HTMLExporter.default_template = 'basic' c = Config(self.site.config['IPYNB_CONFIG']) exportHtml = HTMLExporter(config=c) with io.open(source, "r", encoding="utf8") as in_file: nb_json = nbformat.read(in_file, current_nbformat) (body, resources) = exportHtml.from_notebook_node(nb_json) return body
def compile_html(self, source, dest, is_two_file=True): if flag is None: req_missing(['ipython>=1.1.0'], 'build this site (compile ipynb)') makedirs(os.path.dirname(dest)) HTMLExporter.default_template = 'basic' c = Config(self.site.config['IPYNB_CONFIG']) exportHtml = HTMLExporter(config=c) with io.open(dest, "w+", encoding="utf8") as out_file: with io.open(source, "r", encoding="utf8") as in_file: nb_json = nbformat.read(in_file, current_nbformat) (body, resources) = exportHtml.from_notebook_node(nb_json) out_file.write(body)
def compile_html(self, source, dest, is_two_file=True): if flag is None: req_missing(['ipython[notebook]>=2.0.0'], 'build this site (compile ipynb)') makedirs(os.path.dirname(dest)) HTMLExporter.default_template = 'basic' c = Config(self.site.config['IPYNB_CONFIG']) exportHtml = HTMLExporter(config=c) with io.open(dest, "w+", encoding="utf8") as out_file: with io.open(source, "r", encoding="utf8") as in_file: nb_json = nbformat.read(in_file, current_nbformat) (body, resources) = exportHtml.from_notebook_node(nb_json) out_file.write(body)
def read(self, filepath): filedir = os.path.dirname(filepath) filename = os.path.basename(filepath) _metadata = {} # See if metadata file exists metadata metadata_filename = filename.split('.')[0] + '.ipynb-meta' metadata_filepath = os.path.join(filedir, metadata_filename) if os.path.exists(metadata_filepath): with open(metadata_filepath, 'r') as metadata_file: content = metadata_file.read() metadata_file = open(metadata_filepath) md = markdown.Markdown(extensions=['meta']) md.convert(content) _metadata = md.Meta for key, value in _metadata.items(): _metadata[key] = value[0] else: # Try to load metadata from inside ipython nb ipynb_file = open(filepath) _metadata = json.load(ipynb_file)['metadata'] metadata = {} for key, value in _metadata.items(): key = key.lower() metadata[key] = self.process_metadata(key, value) metadata['ipython'] = True # Converting ipythonnb to html config = Config({ 'CSSHTMLHeaderTransformer': { 'enabled': True, 'highlight_class': '.highlight-ipynb' } }) exporter = HTMLExporter(config=config, template_file='basic', filters={'highlight2html': custom_highlighter}) body, info = exporter.from_filename(filepath) def filter_tags(s): l = s.split('\n') exclude = ['a', '.rendered_html', '@media'] l = [i for i in l if len(list(filter(i.startswith, exclude))) == 0] ans = '\n'.join(l) return STYLE_TAG.format(ans) STYLE_TAG = '<style type=\"text/css\">{0}</style>' css = '\n'.join(filter_tags(css) for css in info['inlining']['css']) css = css + CUSTOM_CSS #body = css + body return body, metadata
def main(ipynb): print("running %s" % ipynb) with io.open(ipynb, encoding='utf8') as f: nb = read(f, NO_CONVERT) test_notebook(nb) base, ext = os.path.splitext(ipynb) exportHtml = HTMLExporter() (body, resources) = exportHtml.from_notebook_node(nb) outfile = ipynb + ".html" open(outfile, 'w').write(encode_utf8(body)) print("wrote %s" % outfile)
def compile_html(self, source, dest, is_two_file=True): if flag is None: raise Exception('To build this site, you need ' 'to install IPython 1.0.') makedirs(os.path.dirname(dest)) HTMLExporter.default_template = 'basic' exportHtml = HTMLExporter() with codecs.open(dest, "w+", "utf8") as out_file: with codecs.open(source, "r", "utf8") as in_file: nb = in_file.read() nb_json = nbformat.reads_json(nb) (body, resources) = exportHtml.from_notebook_node(nb_json) out_file.write(body)
def read(self, filepath): filedir = os.path.dirname(filepath) filename = os.path.basename(filepath) _metadata = {} # See if metadata file exists metadata metadata_filename = filename.split('.')[0] + '.ipynb-meta' metadata_filepath = os.path.join(filedir, metadata_filename) if os.path.exists(metadata_filepath): with open(metadata_filepath, 'r') as metadata_file: content = metadata_file.read() metadata_file = open(metadata_filepath) md = markdown.Markdown(extensions=['meta']) md.convert(content) _metadata = md.Meta for key, value in _metadata.items(): _metadata[key] = value[0] else: # Try to load metadata from inside ipython nb ipynb_file = open(filepath) _metadata = json.load(ipynb_file)['metadata'] metadata = {} for key, value in _metadata.items(): key = key.lower() metadata[key] = self.process_metadata(key, value) metadata['ipython'] = True # Converting ipythonnb to html config = Config({'CSSHTMLHeaderTransformer': {'enabled': True, 'highlight_class': '.highlight-ipynb'}}) exporter = HTMLExporter(config=config, template_file='basic', filters={'highlight2html': custom_highlighter}) body, info = exporter.from_filename(filepath) def filter_tags(s): l = s.split('\n') exclude = ['a', '.rendered_html', '@media'] l = [i for i in l if len(list(filter(i.startswith, exclude))) == 0] ans = '\n'.join(l) return STYLE_TAG.format(ans) STYLE_TAG = '<style type=\"text/css\">{0}</style>' css = '\n'.join(filter_tags(css) for css in info['inlining']['css']) css = css + CUSTOM_CSS #body = css + body return body, metadata
def main(app): static_dir = os.path.join(app.builder.srcdir, '_static') target_dir = os.path.join(app.builder.srcdir, 'notebooks') source_dir = os.path.abspath( os.path.join(app.builder.srcdir, '..', 'notebooks')) rendered_dir = os.path.join(target_dir, 'rendered') if not os.path.exists(static_dir): os.makedirs(static_dir) if not os.path.exists(target_dir): os.makedirs(target_dir) if not os.path.exists(rendered_dir): os.makedirs(rendered_dir) nbroots = [] nbtitles = [] exporter = HTMLExporter(template_file='full') for nb_src in glob.glob(os.path.join(source_dir, '*.ipynb')): print("converting notebook {0}".format(nb_src)) basedir, nbname = os.path.split(nb_src) nb_dest = os.path.join(target_dir, nbname) shutil.copyfile(nb_src, nb_dest) with open(nb_dest, 'r') as f: nb_json = nbformat.reads_json(f.read()) (body, resources) = exporter.from_notebook_node(nb_json) root, ext = os.path.splitext(nbname) nb_html_dest = os.path.join(rendered_dir, root + '.html') with open(nb_html_dest, 'w') as f: f.write(body) nbroots.append(root) nbtitles.append(get_notebook_title(nb_json, root)) for nbroot, nbtitle in zip(nbroots, nbtitles): with open(os.path.join(target_dir, nbroot + '.rst'), 'w') as f: f.write(RST_TEMPLATE.render(title=nbtitle, nbroot=nbroot)) with open(os.path.join(target_dir, 'index.rst'), 'w') as f: f.write( INDEX_TEMPLATE.render(notebooks=nbroots, sphinx_tag='notebook-examples'))
def compile_html(self, source, dest, is_two_file=True): if flag is None: raise Exception('To build this site, you need ' 'to install IPython 1.0.') try: os.makedirs(os.path.dirname(dest)) except: pass HTMLExporter.default_template = 'basic' exportHtml = HTMLExporter() with codecs.open(dest, "w+", "utf8") as out_file: with codecs.open(source, "r", "utf8") as in_file: nb = in_file.read() nb_json = nbformat.reads_json(nb) (body, resources) = exportHtml.from_notebook_node(nb_json) out_file.write(body)
def main(app): static_dir = os.path.join(app.builder.srcdir, '_static') target_dir = os.path.join(app.builder.srcdir, 'notebooks') source_dir = os.path.abspath(os.path.join(app.builder.srcdir, '..', 'notebooks')) rendered_dir = os.path.join(target_dir, 'rendered') if not os.path.exists(static_dir): os.makedirs(static_dir) if not os.path.exists(target_dir): os.makedirs(target_dir) if not os.path.exists(rendered_dir): os.makedirs(rendered_dir) nbroots = [] nbtitles = [] exporter = HTMLExporter(template_file='full') for nb_src in glob.glob(os.path.join(source_dir, '*.ipynb')): print("converting notebook {0}".format(nb_src)) basedir, nbname = os.path.split(nb_src) nb_dest = os.path.join(target_dir, nbname) shutil.copyfile(nb_src, nb_dest) with open(nb_dest, 'r') as f: nb_json = nbformat.reads_json(f.read()) (body, resources) = exporter.from_notebook_node(nb_json) root, ext = os.path.splitext(nbname) nb_html_dest = os.path.join(rendered_dir, root + '.html') with open(nb_html_dest, 'w') as f: f.write(body) nbroots.append(root) nbtitles.append(get_notebook_title(nb_json, root)) for nbroot, nbtitle in zip(nbroots, nbtitles): with open(os.path.join(target_dir, nbroot + '.rst'), 'w') as f: f.write(RST_TEMPLATE.render(title=nbtitle, nbroot=nbroot)) with open(os.path.join(target_dir, 'index.rst'), 'w') as f: f.write(INDEX_TEMPLATE.render(notebooks=nbroots, sphinx_tag='notebook-examples'))
def render(self): try: with open(self.file_path, 'r') as file_pointer: notebook = nbformat.reads(file_pointer.read(), as_version=4) except ValueError: raise exceptions.InvalidFormat('Could not read ipython notebook file.') exporter = HTMLExporter(config=Config({ 'HTMLExporter': { 'template_file': 'basic', }, 'CSSHtmlHeaderTransformer': { 'enabled': False, }, })) (body, _) = exporter.from_notebook_node(notebook) return self.TEMPLATE.render(base=self.assets_url, body=body)
def read(self, filepath): metadata = {} # Files filedir = os.path.dirname(filepath) filename = os.path.basename(filepath) metadata_filename = filename.split('.')[0] + '.ipynb-meta' metadata_filepath = os.path.join(filedir, metadata_filename) # Load metadata if os.path.exists(metadata_filepath): # Metadata is on a external file, process using Pelican MD Reader md_reader = MarkdownReader(self.settings) _content, metadata = md_reader.read(metadata_filepath) else: # Load metadata from ipython notebook file ipynb_file = open(filepath) metadata = json.load(ipynb_file)['metadata'] # Fix metadata to pelican standards for key, value in metadata.items(): del metadata[key] key = key.lower() metadata[key] = self.process_metadata(key, value) metadata['ipython'] = True # Convert ipython notebook to html config = Config({'CSSHTMLHeaderTransformer': {'enabled': True, 'highlight_class': '.highlight-ipynb'}}) exporter = HTMLExporter( config=config, template_file='plugins/ipynb/templates/dsbytes_full', filters={'highlight2html': custom_highlighter} ) content, info = exporter.from_filename(filepath) content = '<html><body>' + content + '</body></html>' # Process using Pelican HTMLReader parser = MyHTMLParser(self.settings, filename) parser.feed(content) parser.close() body = parser.body summary = parser.summary metadata['summary'] = summary return body, metadata
def get_ipython_exporter(): c = IPythonConfig() c.HTMLExporter.template_file = 'basic' c.NbconvertApp.fileext = 'html' c.CSSHTMLHeaderTransformer.enabled = False c.Exporter.filters = {'strip_files_prefix': lambda s: s} #don't strip the files prefix exporter = HTMLExporter(config=c) return exporter
def render(self): try: with open(self.file_path, 'r') as file_pointer: notebook = nbformat.reads(file_pointer.read(), as_version=4) except ValueError: raise exceptions.InvalidFormat( 'Could not read ipython notebook file.') exporter = HTMLExporter(config=Config({ 'HTMLExporter': { 'template_file': 'basic', }, 'CSSHtmlHeaderTransformer': { 'enabled': False, }, })) (body, _) = exporter.from_notebook_node(notebook) return self.TEMPLATE.render(base=self.assets_url, body=body)
def guide_worksheet(path): require_login() from IPython.nbformat import current as nbformat from IPython.nbconvert.exporters import HTMLExporter from IPython.config import Config try: nb = nbformat.reads_json(file("notebooks/"+path).read()) except IOError: return "Файл %s не найден" % (path) config = Config() config.HTMLExporter.template_file = 'basic' config.NbconvertApp.fileext = 'html' config.CSSHTMLHeaderTransformer.enabled = False # don't strip the files prefix - we use it for redirects config.Exporter.filters = {'strip_files_prefix': lambda s: s} C = HTMLExporter(config=config) body=C.from_notebook_node(nb) return dict(content=body[0])
def nb_to_html(nb, template_file = 'basic'): """ Convert notebook `nb` to html, using template `template_file` Parameters ---------- nb : notebook object template_file : str, optional template to use for notebook conversion Returns ------- html : str html output resources : ResourcesDict Resources """ config = Config() config.HTMLExporter.template_file = template_file config.CSSHTMLHeaderTransformer.enabled = False exporter = HTMLExporter(config=config) return exporter.from_notebook_node(nb)
def start(self): super(FormgradeApp, self).start() app.notebook_dir = self.base_directory app.notebook_dir_format = self.directory_format app.exporter = HTMLExporter(config=self.config) url = "http://{:s}:{:d}/".format(self.ip, self.port) self.log.info("Form grader running at {}".format(url)) self.log.info("Use Control-C to stop this server") app.gradebook = Gradebook(self.db_name, ip=self.db_ip, port=self.db_port) app.run(host=self.ip, port=self.port, debug=True, use_reloader=False)
def main(): """ Render STDIN as a notebook. """ exporter = HTMLExporter() json_as_string = sys.stdin.read().decode("utf-8") try: notebook_node = reads_json(json_as_string) except Exception: logging.exception("Unable to parse JSON.") html, _ = exporter.from_notebook_node(notebook_node) sys.stderr.write("JSON was {:,} byte(s); html is {:,} byte(s).\n".format( len(json_as_string), len(html) )) sys.stdout.write(html.encode("utf-8")) sys.stderr.flush() sys.stdout.flush()
def read(self, filepath): filedir = os.path.dirname(filepath) filename = os.path.basename(filepath) _metadata = {} # See if metadata file exists metadata metadata_filename = filename.split('.')[0] + '.ipynb-meta' metadata_filepath = os.path.join(filedir, metadata_filename) if os.path.exists(metadata_filepath): with open(metadata_filepath, 'r') as metadata_file: content = metadata_file.read() metadata_file = open(metadata_filepath) md = markdown.Markdown(extensions=['meta']) md.convert(content) _metadata = md.Meta for key, value in _metadata.items(): _metadata[key] = value[0] else: # Try to load metadata from inside ipython nb ipynb_file = open(filepath) _metadata = json.load(ipynb_file)['metadata'] metadata = {} for key, value in _metadata.items(): key = key.lower() metadata[key] = self.process_metadata(key, value) metadata['ipython'] = True # Converting ipythonnb to html config = Config({'CSSHTMLHeaderTransformer': {'enabled': True}}) here = os.path.dirname(os.path.realpath(__file__)) loader = FileSystemLoader(here) exporter = HTMLExporter(config=config, extra_loaders=[loader], template_file='content.tpl') body, info = exporter.from_filename(filepath) return body, metadata
def main(): args = parse_arg() config = Config() if args.template != default_template: app_log.info("Using custom template: %s", args.template) config.HTMLExporter.template_file = args.template config.NbconvertApp.fileext = 'html' # config.CSSHTMLHeaderTransformer.enabled = False template_path = pjoin(here, 'templates') static_path = pjoin(here, 'static') document_path = current_path exporter = HTMLExporter(config=config, log=app_log) env = Environment(loader=FileSystemLoader(template_path)) env.filters['markdown'] = markdown.markdown # notebook notebook_manager = FileNotebookManager(notebook_dir=current_path, log=app_log) settings = dict( jinja2_env=env, static_path=static_path, exporter=exporter, config=config, pool=ThreadPoolExecutor(threads), gzip=True, log=app_log, render_timeout=20, document_path=document_path, notebook_manager=notebook_manager, ) app = web.Application(handlers, debug=True, **settings) http_server = httpserver.HTTPServer(app, xheaders=True) app_log.info("Listening on port %i", args.port) http_server.listen(args.port) instance = ioloop.IOLoop.instance() tornado.autoreload.start(instance) instance.start()
def start(self): super(FormgradeApp, self).start() # Init authenticator. self.authenticator_instance = self.authenticator_class( app, self.ip, self.port, self.base_directory, parent=self) app.auth = self.authenticator_instance # now launch the formgrader app.notebook_dir = self.base_directory app.notebook_dir_format = self.directory_structure app.nbgrader_step = self.autograded_directory app.exporter = HTMLExporter(config=self.config) app.mathjax_url = self.mathjax_url url = "http://{:s}:{:d}/".format(self.ip, self.port) self.log.info("Form grader running at {}".format(url)) self.log.info("Use Control-C to stop this server") app.gradebook = Gradebook(self.db_url) app.run(host=self.ip, port=self.port, debug=True, use_reloader=False)
def notebook(preprocessor, tag, markup): match = FORMAT.search(markup) if match: argdict = match.groupdict() src = argdict['src'] start = argdict['start'] end = argdict['end'] else: raise ValueError("Error processing input, " "expected syntax: {0}".format(SYNTAX)) if start: start = int(start) else: start = 0 if end: end = int(end) else: end = None settings = preprocessor.configs.config['settings'] nb_dir = settings.get('NOTEBOOK_DIR', 'notebooks') nb_path = os.path.join('content', nb_dir, src) if not os.path.exists(nb_path): raise ValueError("File {0} could not be found".format(nb_path)) # Create the custom notebook converter c = Config({'CSSHTMLHeaderTransformer': {'enabled':True, 'highlight_class':'.highlight-ipynb'}, 'SubCell': {'enabled':True, 'start':start, 'end':end}}) exporter = HTMLExporter(config=c, template_file='basic', filters={'highlight2html': custom_highlighter}, transformers=[SubCell], extra_loaders=[pelican_loader]) # read and parse the notebook with open(nb_path) as f: nb_text = f.read() nb_json = nbformat.reads_json(nb_text) (body, resources) = exporter.from_notebook_node(nb_json) # if we haven't already saved the header, save it here. if not notebook.header_saved: print ("\n ** Writing styles to _nb_header.html: " "this should be included in the theme. **\n") header = '\n'.join(CSS_WRAPPER.format(css_line) for css_line in resources['inlining']['css']) header += JS_INCLUDE with open('_nb_header.html', 'w') as f: f.write(header) notebook.header_saved = True # this will stash special characters so that they won't be transformed # by subsequent processes. body = preprocessor.configs.htmlStash.store(body, safe=True) return body
def notebook(preprocessor, tag, markup): match = FORMAT.search(markup) if match: argdict = match.groupdict() src = argdict['src'] start = argdict['start'] end = argdict['end'] language = argdict['language'] else: raise ValueError("Error processing input, " "expected syntax: {0}".format(SYNTAX)) if start: start = int(start) else: start = 0 if end: end = int(end) else: end = None language_applied_highlighter = partial(custom_highlighter, language=language) nb_dir = preprocessor.configs.getConfig('NOTEBOOK_DIR') nb_path = os.path.join('content', nb_dir, src) if not os.path.exists(nb_path): raise ValueError("File {0} could not be found".format(nb_path)) # Create the custom notebook converter c = Config({'CSSHTMLHeaderTransformer': {'enabled':True, 'highlight_class':'.highlight-ipynb'}, 'SubCell': {'enabled':True, 'start':start, 'end':end}}) template_file = 'basic' if IPYTHON_VERSION >= 3: if os.path.exists('pelicanhtml_3.tpl'): template_file = 'pelicanhtml_3' elif IPYTHON_VERSION == 2: if os.path.exists('pelicanhtml_2.tpl'): template_file = 'pelicanhtml_2' else: if os.path.exists('pelicanhtml_1.tpl'): template_file = 'pelicanhtml_1' if IPYTHON_VERSION >= 2: subcell_kwarg = dict(preprocessors=[SubCell]) else: subcell_kwarg = dict(transformers=[SubCell]) exporter = HTMLExporter(config=c, template_file=template_file, filters={'highlight2html': language_applied_highlighter}, **subcell_kwarg) # read and parse the notebook with open(nb_path) as f: nb_text = f.read() if IPYTHON_VERSION < 3: nb_json = IPython.nbformat.current.reads_json(nb_text) else: nb_json = IPython.nbformat.reads(nb_text, as_version=4) (body, resources) = exporter.from_notebook_node(nb_json) # if we haven't already saved the header, save it here. if not notebook.header_saved: print ("\n ** Writing styles to _nb_header.html: " "this should be included in the theme. **\n") header = '\n'.join(CSS_WRAPPER.format(css_line) for css_line in resources['inlining']['css']) header += JS_INCLUDE with open('_nb_header.html', 'w') as f: f.write(header) notebook.header_saved = True # this will stash special characters so that they won't be transformed # by subsequent processes. body = preprocessor.configs.htmlStash.store(body, safe=True) return body
config = { 'CACHE_TYPE': 'lib.MemcachedMultipart.multipartmemecached', 'CACHE_MEMCACHED_SERVERS': servers, 'CACHE_MEMCACHED_PASSWORD': password[0], 'CACHE_MEMCACHED_USERNAME': username[0] } cache = Cache(app, config=config) from IPython.config import Config config = Config() config.HTMLExporter.template_file = 'basic' config.NbconvertApp.fileext = 'html' config.CSSHtmlHeaderTransformer.enabled = False C = HTMLExporter(config=config) minutes = 60 hours = 60 * minutes import newrelic.agent def nrhead(): return newrelic.agent.get_browser_timing_header() def nrfoot(): return newrelic.agent.get_browser_timing_footer()
def main(): # command-line options define("debug", default=False, help="run in debug mode", type=bool) define("no_cache", default=False, help="Do not cache results", type=bool) define( "localfiles", default="", help= "Allow to serve local files under /localfile/* this can be a security risk", type=str) define("port", default=5000, help="run on the given port", type=int) define("cache_expiry_min", default=10 * 60, help="minimum cache expiry (seconds)", type=int) define("cache_expiry_max", default=2 * 60 * 60, help="maximum cache expiry (seconds)", type=int) define("mc_threads", default=1, help="number of threads to use for Async Memcache", type=int) define("threads", default=1, help="number of threads to use for rendering", type=int) define("processes", default=0, help="use processes instead of threads for rendering", type=int) define("frontpage", default=FRONTPAGE_JSON, help="path to json file containing frontpage content", type=str) define("sslcert", help="path to ssl .crt file", type=str) define("sslkey", help="path to ssl .key file", type=str) tornado.options.parse_command_line() # NBConvert config config = Config() config.HTMLExporter.template_file = 'basic' config.NbconvertApp.fileext = 'html' config.CSSHTMLHeaderTransformer.enabled = False # don't strip the files prefix - we use it for redirects # config.Exporter.filters = {'strip_files_prefix': lambda s: s} # DEBUG env implies both autoreload and log-level if os.environ.get("DEBUG"): options.debug = True logging.getLogger().setLevel(logging.DEBUG) # setup memcache mc_pool = ThreadPoolExecutor(options.mc_threads) if options.processes: # can't pickle exporter instances, exporter = HTMLExporter pool = ProcessPoolExecutor(options.processes) else: exporter = HTMLExporter(config=config, log=log.app_log) pool = ThreadPoolExecutor(options.threads) memcache_urls = os.environ.get('MEMCACHIER_SERVERS', os.environ.get('MEMCACHE_SERVERS')) # Handle linked Docker containers if (os.environ.get('NBCACHE_PORT')): tcp_memcache = os.environ.get('NBCACHE_PORT') memcache_urls = tcp_memcache.split('tcp://')[1] if options.no_cache: log.app_log.info("Not using cache") cache = MockCache() elif pylibmc and memcache_urls: kwargs = dict(pool=mc_pool) username = os.environ.get('MEMCACHIER_USERNAME', '') password = os.environ.get('MEMCACHIER_PASSWORD', '') if username and password: kwargs['binary'] = True kwargs['username'] = username kwargs['password'] = password log.app_log.info("Using SASL memcache") else: log.app_log.info("Using plain memecache") cache = AsyncMultipartMemcache(memcache_urls.split(','), **kwargs) else: log.app_log.info("Using in-memory cache") cache = DummyAsyncCache() # setup tornado handlers and settings template_path = pjoin(here, 'templates') static_path = pjoin(here, 'static') env = Environment(loader=FileSystemLoader(template_path)) env.filters['markdown'] = markdown.markdown try: git_data = git_info(here) except Exception as e: app_log.error("Failed to get git info: %s", e) git_data = {} else: git_data['msg'] = escape(git_data['msg']) if options.no_cache: # force jinja to recompile template every time env.globals.update(cache_size=0) env.globals.update( nrhead=nrhead, nrfoot=nrfoot, git_data=git_data, ipython_info=ipython_info(), len=len, ) AsyncHTTPClient.configure(HTTPClientClass) client = AsyncHTTPClient() github_client = AsyncGitHubClient(client) # load frontpage sections try: with io.open(options.frontpage, 'r') as f: frontpage_sections = json.load(f) except IOError: frontpage_sections = [] # cache frontpage links for the maximum allowed time max_cache_uris = {''} for section in frontpage_sections: for link in section['links']: max_cache_uris.add('/' + link['target']) settings = dict( log_function=log_request, jinja2_env=env, static_path=static_path, client=client, github_client=github_client, exporter=exporter, config=config, cache=cache, cache_expiry_min=options.cache_expiry_min, cache_expiry_max=options.cache_expiry_max, max_cache_uris=max_cache_uris, frontpage_sections=frontpage_sections, pool=pool, gzip=True, render_timeout=20, localfile_path=os.path.abspath(options.localfiles), fetch_kwargs=dict(connect_timeout=10, ), ) # create and start the app if options.localfiles: log.app_log.warning( "Serving local notebooks in %s, this can be a security risk", options.localfiles) # use absolute or relative paths: handlers.insert(0, (r'/localfile/(.*)', LocalFileHandler)) handlers.insert(0, (r'/localfile/(.*)/', LocalFileHandler)) # load ssl options ssl_options = None if options.sslcert: ssl_options = { 'certfile': options.sslcert, 'keyfile': options.sslkey, } app = web.Application(handlers, debug=options.debug, **settings) http_server = httpserver.HTTPServer(app, xheaders=True, ssl_options=ssl_options) log.app_log.info("Listening on port %i", options.port) http_server.listen(options.port) ioloop.IOLoop.instance().start()
def read(self, filepath): metadata = {} # Files filedir = os.path.dirname(filepath) filename = os.path.basename(filepath) metadata_filename = filename.split('.')[0] + '.ipynb-meta' metadata_filepath = os.path.join(filedir, metadata_filename) # Load metadata if os.path.exists(metadata_filepath): # Metadata is on a external file, process using Pelican MD Reader md_reader = MarkdownReader(self.settings) _content, metadata = md_reader.read(metadata_filepath) else: # Load metadata from ipython notebook file ipynb_file = open(filepath) metadata = json.load(ipynb_file)['metadata'] # Fix metadata to pelican standards for key, value in metadata.items(): del metadata[key] key = key.lower() metadata[key] = self.process_metadata(key, value) metadata['ipython'] = True # Convert ipython notebook to html config = Config({ 'CSSHTMLHeaderTransformer': { 'enabled': True, 'highlight_class': '.highlight-ipynb' } }) exporter = HTMLExporter(config=config, template_file='basic', filters={'highlight2html': custom_highlighter}) content, info = exporter.from_filename(filepath) if BeautifulSoup: soup = BeautifulSoup(content) for i in soup.findAll("div", {"class": "input"}): if i.findChildren()[1].find(text='#ignore') is not None: i.extract() else: soup = content # Process using Pelican HTMLReader content = '<body>{0}</body>'.format( soup) # So Pelican HTMLReader works parser = MyHTMLParser(self.settings, filename) parser.feed(content) parser.close() body = parser.body if ('IPYNB_USE_META_SUMMARY' in self.settings.keys() and \ self.settings['IPYNB_USE_META_SUMMARY'] == False) or \ 'IPYNB_USE_META_SUMMARY' not in self.settings.keys(): metadata['summary'] = parser.summary def filter_css(style_text): ''' HACK: IPython returns a lot of CSS including its own bootstrap. Get only the IPython Notebook CSS styles. ''' index = style_text.find('/*!\n*\n* IPython notebook\n*\n*/') if index > 0: style_text = style_text[index:] index = style_text.find('/*!\n*\n* IPython notebook webapp\n*\n*/') if index > 0: style_text = style_text[:index] style_text = re.sub(r'color\:\#0+(;)?', '', style_text) style_text = re.sub( r'\.rendered_html[a-z0-9,._ ]*\{[a-z0-9:;%.#\-\s\n]+\}', '', style_text) return '<style type=\"text/css\">{0}</style>'.format(style_text) ipython_css = '\n'.join( filter_css(css_style) for css_style in info['inlining']['css']) body = ipython_css + body + LATEX_CUSTOM_SCRIPT return body, metadata
def notebook(preprocessor, tag, markup): match = FORMAT.search(markup) if match: argdict = match.groupdict() src = argdict['src'] start = argdict['start'] end = argdict['end'] else: raise ValueError("Error processing input, " "expected syntax: {0}".format(SYNTAX)) if start: start = int(start) else: start = 0 if end: end = int(end) else: end = None settings = preprocessor.configs.config['settings'] nb_dir = settings.get('NOTEBOOK_DIR', 'notebooks') nb_path = os.path.join('content', nb_dir, src) if not os.path.exists(nb_path): raise ValueError("File {0} could not be found".format(nb_path)) # Create the custom notebook converter c = Config({'CSSHTMLHeaderTransformer': {'enabled':True, 'highlight_class':'.highlight-ipynb'}, 'SubCell': {'enabled':True, 'start':start, 'end':end}}) template_file = 'basic' if LooseVersion(IPython.__version__) >= '2.0': if os.path.exists('pelicanhtml_2.tpl'): template_file = 'pelicanhtml_2' else: if os.path.exists('pelicanhtml_1.tpl'): template_file = 'pelicanhtml_1' if LooseVersion(IPython.__version__) >= '2.0': subcell_kwarg = dict(preprocessors=[SubCell]) else: subcell_kwarg = dict(transformers=[SubCell]) exporter = HTMLExporter(config=c, template_file=template_file, filters={'highlight2html': custom_highlighter}, **subcell_kwarg) # read and parse the notebook with open(nb_path) as f: nb_text = f.read() nb_json = nbformat.reads_json(nb_text) (body, resources) = exporter.from_notebook_node(nb_json) for h in '123456': body = body.replace('<h%s' % h, '<h%s class="ipynb"' % h) body = '<div class="ipynb">\n\n' + body + "\n\n</div>" # if we haven't already saved the header, save it here. if not notebook.header_saved: print ("\n ** Writing styles to _nb_header.html: " "this should be included in the theme. **\n") header = '\n'.join(CSS_WRAPPER.format(css_line) for css_line in resources['inlining']['css']) header += JS_INCLUDE # # replace the highlight tags header = header.replace('highlight', 'highlight-ipynb') header = header.replace('html, body', '\n'.join(('pre.ipynb {', ' color: black;', ' background: #f7f7f7;', ' border: 0;', ' box-shadow: none;', ' margin-bottom: 0;', ' padding: 0;' '}\n', 'html, body'))) # # create a special div for notebook header = header.replace('body {', 'div.ipynb {') header = header.replace('body{margin:0;font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:13px;line-height:20px;color:#000;background-color:#fff}', '') # # specialize headers header = header.replace('html, body,', '\n'.join((('h1.ipynb h2.ipynb h3.ipynb ' 'h4.ipynb h5.ipynb h6.ipynb {'), 'h1.ipynb h2.ipynb ... {', ' margin: 0;', ' padding: 0;', ' border: 0;', ' font-size: 100%;', ' font: inherit;', ' vertical-align: baseline;', '}\n', 'html, body,'))) # # # # comment out document-level formatting header = header.replace('html, body,', '/*html, body,*/') header = header.replace('body{background-color:#fff}', '/*body{background-color:#fff}*/') header = header.replace('body{background-color:#fff;position:absolute;left:0;right:0;top:0;bottom:0;overflow:visible}', '/*body{background-color:#fff;position:absolute;left:0;right:0;top:0;bottom:0;overflow:visible}*/') header = header.replace('h1, h2, h3, h4, h5, h6,', '/*h1, h2, h3, h4, h5, h6,*/') with open('_nb_header.html', 'w') as f: f.write(header) notebook.header_saved = True # this will stash special characters so that they won't be transformed # by subsequent processes. body = preprocessor.configs.htmlStash.store(body, safe=True) return body
def read(self, filepath): metadata = {} # Files filedir = os.path.dirname(filepath) filename = os.path.basename(filepath) metadata_filename = filename.split('.')[0] + '.ipynb-meta' metadata_filepath = os.path.join(filedir, metadata_filename) # If filename starts with draft, set the status accordingly if filename.lower().startswith('draft'): metadata['status'] = 'draft' with open(filepath) as f: nb = nbformat.read(f, 'ipynb') # readin ipynb content first_cell = nb.worksheets[0].cells[0] # Read in metadata metadata = join_metadata(metadata, nb.metadata) if 'pelican' in first_cell.metadata: m = first_cell.metadata['pelican'] metadata = join_metadata(metadata, m) if os.path.exists(metadata_filepath): # Metadata is on a external file, process using Pelican MD Reader md_reader = MarkdownReader(self.settings) _content, m = md_reader.read(metadata_filepath) metadata = join_metadata(metadata, m) # Reformat metadata into pelican acceptable format for k, v in metadata.items(): del metadata[k] k = k.lower() metadata[k] = self.process_metadata(k, v) metadata['ipython'] = True # use first cell as the title if flag is set field = 'IPYNB_FIRST_CELL_HEADING_AS_TITLE' if self.settings.get(field, False) and first_cell.cell_type == 'heading': metadata['title'] = first_cell.source # Truncate the first cell from notebook nb.worksheets[0].cells = nb.worksheets[0].cells[1:] # Convert ipython notebook to html config = Config({'CSSHTMLHeaderPreprocessor': {'enabled': True, 'highlight_class': '.highlight-ipynb'}}) exporter = HTMLExporter(config=config, template_file='basic', filters={'highlight2html': custom_highlighter}) content, info = exporter.from_notebook_node(nb) if BeautifulSoup: soup = BeautifulSoup(content) for i in soup.findAll("div", {"class" : "input"}): if i.findChildren()[1].find(text='#ignore') is not None: i.extract() else: soup = content content = '<body>{0}</body>'.format(soup) # So Pelican HTMLReader works parser = MyHTMLParser(self.settings, filename) parser.feed(content) parser.close() body = parser.body summary = parser.summary field = 'IPYNB_FIRST_CONTENT_AS_SUMMARY' first_cell = nb.worksheets[0].cells[0] if self.settings.get(field, False) and first_cell.cell_type == 'markdown': raw = nb.worksheets[0].cells[0].source md = markdown.Markdown() metadata['summary'] = md.convert(raw) else: metadata['summary'] = summary # Remove some CSS styles, so it doesn't break the theme. def filter_tags(style_text): style_list = style_text.split('\n') exclude = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a', 'ul', 'ol', 'li', '.rendered_html', '@media', '.navbar', 'nav.navbar', '.navbar-text', 'code', 'pre', 'div.text_cell_render'] style_list = [i for i in style_list if len(list(filter(i.startswith, exclude))) == 0] ans = '\n'.join(style_list) return '<style type=\"text/css\">{0}</style>'.format(ans) css = '\n'.join(filter_tags(css) for css in info['inlining']['css']) css = css + CUSTOM_CSS body = css + body return body, metadata
def __init__(self): QtGui.QMainWindow.__init__(self) self.tabs = QtGui.QTabWidget(self, tabsClosable=True, movable=True, elideMode=QtCore.Qt.ElideRight, currentChanged=self.currentTabChanged, tabCloseRequested=self.closeTabRequested) self.bottom = QtWebKit.QWebView(self) self.setWindowTitle("vIPer") self.splitter = QtGui.QSplitter(self) self.setCentralWidget(self.splitter) self.splitter.addWidget(self.tabs) self.splitter.addWidget(self.bottom) self.bottom.setVisible(False) self.tabWidgets = [] self.titleHistory = [] self.path = QtCore.QDir.currentPath() self.newtab = QtGui.QAction(QtGui.QIcon.fromTheme("document-new"), "New Tab", self, triggered=self.newTabTriggered, shortcut="Ctrl+t") self.htmler = QtGui.QAction(QtGui.QIcon.fromTheme("go-down"), "Htmler", self, triggered=self.screenHtmled, shortcut="Ctrl+j") self.slider = QtGui.QAction(QtGui.QIcon.fromTheme("go-jump"), "Slider", self, triggered=self.screenSlided, shortcut="Ctrl+k") self.splitterV = QtGui.QAction( QtGui.QIcon.fromTheme("object-flip-vertical"), "Split vertically", self, triggered=self.screenSplittedVhtml, shortcut="F9") self.splitterV.setMenu(QtGui.QMenu()) self.splitterV.menu().addAction( QtGui.QAction('HTML', self, triggered=self.screenSplittedVhtml)) self.splitterV.menu().addAction( QtGui.QAction('SLIDE', self, triggered=self.screenSplittedVslide)) self.splitterH = QtGui.QAction( QtGui.QIcon.fromTheme("object-flip-horizontal"), "Split horizontally", self, triggered=self.screenSplittedHhtml, shortcut="F10") self.splitterH.setMenu(QtGui.QMenu()) self.splitterH.menu().addAction( QtGui.QAction('HTML', self, triggered=self.screenSplittedHhtml)) self.splitterH.menu().addAction( QtGui.QAction('SLIDE', self, triggered=self.screenSplittedHslide)) self.recorder = QtGui.QAction( QtGui.QIcon.fromTheme("media-playback-start"), "Record", self, triggered=self.screenRecorded, shortcut="Ctrl+r") self.stopper = QtGui.QAction( QtGui.QIcon.fromTheme("media-playback-stop"), "Stop", self, triggered=self.screenStopped, shortcut="Ctrl+Alt+s") self.addAction( QtGui.QAction("Split Screen", self, checkable=True, toggled=self.splitToggled, shortcut="F11")) self.addAction( QtGui.QAction("Full Screen", self, checkable=True, toggled=self.screenToggled, shortcut="F12")) self.helper = QtGui.QAction(QtGui.QIcon.fromTheme("help-faq"), "Help", self, triggered=self.newHelpTabTriggered, shortcut="Ctrl+h") self.full = HTMLExporter() self.html = '.html' self.rev = SlidesExporter() self.rev_html = '.slides.html' self.server = ServePostProcessor() self.servePool = [] self.horizontal = QtCore.Qt.Horizontal self.vertical = QtCore.Qt.Vertical self.addTab(QtCore.QUrl('http://127.0.0.1:8888/'))
def read(self, filepath): metadata = {} # Files filedir = os.path.dirname(filepath) filename = os.path.basename(filepath) metadata_filename = filename.split('.')[0] + '.ipynb-meta' metadata_filepath = os.path.join(filedir, metadata_filename) # Load metadata if os.path.exists(metadata_filepath): # Metadata is on a external file, process using Pelican MD Reader md_reader = MarkdownReader(self.settings) _content, metadata = md_reader.read(metadata_filepath) else: # Load metadata from ipython notebook file ipynb_file = open(filepath) metadata = json.load(ipynb_file)['metadata'] # Fix metadata to pelican standars for key, value in metadata.items(): del metadata[key] key = key.lower() metadata[key] = self.process_metadata(key, value) metadata['ipython'] = True # Convert ipython notebook to html config = Config({'CSSHTMLHeaderTransformer': {'enabled': True, 'highlight_class': '.highlight-ipynb'}}) exporter = HTMLExporter(config=config, template_file='basic', filters={'highlight2html': custom_highlighter}) content, info = exporter.from_filename(filepath) soup = BeautifulSoup(content) # find all the inputs with hide and strip them out inputs = 0 for i in soup.findAll("div", {"class" : "input"}): if i.findChildren()[1].findChild().findChild().findChild().find(text='#HIDE') != None: i.extract() else: inputs = inputs+1 tag = soup.new_tag('a', href="javascript:toggle('input%s');" % inputs , target='_self') tag.string = i.findChildren()[0].text.strip() i.findChildren()[0].clear() i.findChildren()[0].append(tag) try: i.find("div", {"class" :"input_area"})['id'] = 'input%s' %inputs except: print i # Process using Pelican HTMLReader content = '<body>{0}</body>'.format(unicode(soup)) # So Pelican HTMLReader works parser = MyHTMLParser(self.settings, filename) parser.feed(content) parser.close() body = parser.body summary = parser.summary metadata['summary'] = summary # Remove some CSS styles, so it doesn't break the themes. def filter_tags(style_text): style_list = style_text.split('\n') exclude = ['body','p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a', 'ul', 'ol', 'li', '.rendered_html', '@media', '.navbar', 'nav.navbar', '.navbar-text', 'code', 'pre', 'div.text_cell_render','body'] style_list = [i for i in style_list if len(list(filter(i.startswith, exclude))) == 0] ans = '\n'.join(style_list) return '<style type=\"text/css\">{0}</style>'.format(ans) css = '\n'.join(filter_tags(css) for css in info['inlining']['css']) css = CUSTOM_CSS body = css + body body = body + js return body, metadata
def get(self, user, filename): ## filename can have a path on it next = "/hub/%s/public/%s" % (user, filename) filesystem_path = "/home/%s/Public/%s" % (user, filename) if os.path.isfile(filesystem_path): # download, raw, or view notebook command = "view" if len(self.get_arguments("view")) > 0: command = "view" elif len(self.get_arguments("download")) > 0: command = "download" elif len(self.get_arguments("copy")) > 0: command = "copy" elif len(self.get_arguments("pdf")) > 0: command = "pdf" elif len(self.get_arguments("raw")) > 0: command = "raw" # else: view if filename.endswith(".ipynb"): if command in ["view", "pdf"]: # first, make a notebook html: #with open("/home/%s/Public/%s" % (user, filename)) as fp: # notebook_content = fp.read() if command == "view": exporter = HTMLExporter(template_file='full') else: exporter = PDFExporter(latex_count=1) nb_json = nbformat.read("/home/%s/Public/%s" % (user, filename), as_version=4) #if command == "pdf": # # If pdf, remove heading numbering: # for cell in nb_json["worksheets"][0]["cells"]: # if cell["cell_type"] == "heading": # cell["source"] = re.sub("^([0-9]+\.?)+\s", "", cell["source"]) # where to get css, images? if command == "pdf": self.set_header('Content-Type', "application/pdf") base_filename = os.path.basename(filename) self.set_header('Content-Disposition', 'attachment; filename="%s"' % base_filename) else: # render as HTML # add header/footer: path = "/hub/%s/public" % user parts = [(path, path)] for part in filename.split("/")[:-1]: path += "/" + part parts.append((path, part)) breadcrumbs = " / ".join(map(lambda pair: '<a href="%s" target="_blank">%s</a>' % pair, parts)) env = { "breadcrumbs": breadcrumbs, "url": path + "/" + filename + "?download" } cell = new_markdown_cell(source=""" <table width="100%" style="border: none;"> <tr style="border: none;"> <td style="border: none;" width="100px"> <img src="https://serendip.brynmawr.edu/oneworld/files/styles/thumbnail/public/pictures/SerendipStudioAvatar.png?itok=48Z_omRv"/> </td> <td style="border: none;" width="50%"> <h2><a href="https://serendip.brynmawr.edu/oneworld/tides/explore">TIDES: Teaching to Increase Diversity and Equity in STEM</a></h2> </td> <td style="border: none;"> <a href="http://jupyter.physics.brynmawr.edu/hub/dblank/public/Jupyter%20Help.ipynb" title="Help"> <span class='fa fa-info-circle fa-2x menu-icon'></span> <span class='menu-text'>Help</span> </a> </td> <td style="border: none;"> <a href="{url}" title="Download Notebook" download> <span class='fa fa-download fa-2x menu-icon'></span> <span class='menu-text'>Download Notebook</span> </a> </td> </tr> <tr style="border: none;"> <td colspan="4" style="border: none;"> <b>Public notebooks:</b> {breadcrumbs} </td> </tr> </table> <hr style="background-color: #534f9a; height: 5px; border: 0; "> """.format(**env)) nb_json["cells"].insert(0, cell) (body, resources) = exporter.from_notebook_node(nb_json) self.write(body) elif command == "download": # download notebook json self.download(user, filename, "text/plain") elif command == "copy": # copy notebook json, if logged in if self.get_current_user_name(): self.copy_file(user, filename, self.get_current_user_name()) else: self.write("Please <a href=\"/hub/login?next=%s\">login</a> to allow copy." % next) else: # raw, just get file contents with open("/home/%s/Public/%s" % (user, filename), "rb") as fp: self.write(fp.read()) else: # some other kind of file # FIXME: how to get all of custom stuff? if command == "copy": if self.get_current_user_name(): self.copy_file(user, filename, self.get_current_user_name()) else: self.write("Please <a href=\"/hub/login?next=%s\">login</a> to allow copy." % next) else: # whatever, just get or download it base_filename = os.path.basename(filename) base, ext = os.path.splitext(base_filename) app_log.info("extension is: %s" % ext) if base_filename == "custom.css": file_path = "/home/%s/.ipython/profile_default/static/custom/custom.css" % user self.set_header('Content-Type', "text/css") with open(file_path, "rb") as fp: self.write(fp.read()) elif ext in [".txt", ".html", ".js", ".css", ".pdf", ".gif", ".jpeg", ".jpg", ".png"]: # show in browser app_log.info("mime: %s" % str(mimetypes.guess_type(filename)[0])) self.set_header('Content-Type', mimetypes.guess_type(filename)[0]) with open("/home/%s/Public/%s" % (user, filename), "rb") as fp: self.write(fp.read()) else: self.download(user, filename) else: # not a file; directory listing # filename can have a full path, and might be empty url_path = "/hub/%s/public" % user # could be: images, images/, images/subdir, images/subdir/ if not filename.endswith("/") and filename.strip() != "": filename += "/" files = glob.glob("/home/%s/Public/%s*" % (user, filename)) self.write("<h1>Jupyter Project at Bryn Mawr College</h1>\n") self.write("[<a href=\"/hub/login\">Home</a>] ") if self.get_current_user_name(): self.write("[<a href=\"/user/%(current_user)s/tree\">%(current_user)s</a>] " % {"current_user": self.get_current_user_name()}) self.write("<p/>\n") self.write("<p>Public files for <b>/hub/%s/public/%s</b>:</p>\n" % (user, filename)) self.write("<ol>\n") for absolute_filename in sorted(files): if os.path.isdir(absolute_filename): dir_path = absolute_filename.split("/") dir_name = dir_path[-1] public_path = "/".join(dir_path[dir_path.index("Public") + 1:]) self.write("<li><a href=\"%(url_path)s/%(public_path)s\">%(dir_name)s</a></li>\n" % {"url_path": url_path, "dir_name": dir_name, "public_path": public_path}) else: file_path, filename = absolute_filename.rsplit("/", 1) dir_path = absolute_filename.split("/") public_path = "/".join(dir_path[dir_path.index("Public") + 1:]) variables = {"user": user, "filename": filename, "url_path": url_path, "next": next, "public_path": public_path} if filename.endswith(".ipynb"): if self.get_current_user_name(): self.write(("<li><a href=\"%(url_path)s/%(public_path)s\">%(filename)s</a> "+ "(<a href=\"%(url_path)s/%(public_path)s?raw\">raw</a>, " + "<a href=\"%(url_path)s/%(public_path)s?download\">download</a>, " + "<a href=\"%(url_path)s/%(public_path)s?copy\">copy</a>" + ((", <a href=\"/user/%s/notebooks/Public/%s\">edit</a>" % (user, filename)) if self.get_current_user_name() == user else ", edit") + ")</li>\n") % variables) else: self.write(("<li><a href=\"%(url_path)s/%(public_path)s\">%(filename)s</a> "+ "(<a href=\"%(url_path)s/%(public_path)s?raw\">raw</a>, " + "<a href=\"%(url_path)s/%(public_path)s?download\">download</a>, " + "copy, edit) " + "[<a href=\"/hub/login?next=%(next)s\">login</a> to copy]</li>\n") % variables) else: # some other kind of file (eg, .zip, .css): self.write("<li><a href=\"%(url_path)s/%(public_path)s\">%(filename)s</a></li>\n" % variables) self.write("</ol>\n") self.write("<hr>\n") self.write("<p><i>Please see <a href=\"/hub/dblank/public/Jupyter Help.ipynb\">Jupyter Help</a> for more information about this server.</i></p>\n")
def notebook(preprocessor, tag, markup): match = FORMAT.search(markup) if match: argdict = match.groupdict() src = argdict['src'] start = argdict['start'] end = argdict['end'] language = argdict['language'] else: raise ValueError("Error processing input, " "expected syntax: {0}".format(SYNTAX)) if start: start = int(start) else: start = 0 if end: end = int(end) else: end = None language_applied_highlighter = partial(custom_highlighter, language=language) nb_dir = preprocessor.configs.getConfig('NOTEBOOK_DIR') nb_path = os.path.join('content', nb_dir, src) if not os.path.exists(nb_path): raise ValueError("File {0} could not be found".format(nb_path)) # Create the custom notebook converter c = Config({ 'CSSHTMLHeaderTransformer': { 'enabled': True, 'highlight_class': '.highlight-ipynb' }, 'SubCell': { 'enabled': True, 'start': start, 'end': end } }) template_file = 'basic' if LooseVersion(IPython.__version__) >= '2.0': if os.path.exists('pelicanhtml_2.tpl'): template_file = 'pelicanhtml_2' else: if os.path.exists('pelicanhtml_1.tpl'): template_file = 'pelicanhtml_1' if LooseVersion(IPython.__version__) >= '2.0': subcell_kwarg = dict(preprocessors=[SubCell]) else: subcell_kwarg = dict(transformers=[SubCell]) exporter = HTMLExporter( config=c, template_file=template_file, filters={'highlight2html': language_applied_highlighter}, **subcell_kwarg) # read and parse the notebook with open(nb_path) as f: nb_text = f.read() nb_json = nbformat.reads_json(nb_text) (body, resources) = exporter.from_notebook_node(nb_json) # To make the higlighting work with Ipython 2.X resources['inlining']['css'][1] = resources['inlining']['css'][1].replace( 'highlight', 'highlight-ipynb') # if we haven't already saved the header, save it here. if not notebook.header_saved: print("\n ** Writing styles to _nb_header.html: " "this should be included in the theme. **\n") header = '\n'.join( CSS_WRAPPER.format(css_line) for css_line in resources['inlining']['css']) header += JS_INCLUDE with open('_nb_header.html', 'w') as f: f.write(header) notebook.header_saved = True # this will stash special characters so that they won't be transformed # by subsequent processes. body = preprocessor.configs.htmlStash.store(body, safe=True) return body
from .. import FileRenderer import re import os.path from IPython.config import Config # from IPython.nbconvert import export_python from IPython.nbconvert.exporters import HTMLExporter from IPython.nbformat import current as nbformat c = Config() c.HTMLExporter.template_file = 'basic' c.NbconvertApp.fileext = 'html' c.CSSHTMLHeaderTransformer.enabled = False c.Exporter.filters = { 'strip_files_prefix': lambda s: s } #don't strip the files prefix exporter = HTMLExporter(config=c) class NbFormatError(Exception): pass class IPynbRenderer(FileRenderer): def _detect(self, file_pointer): _, ext = os.path.splitext(file_pointer.name) return ext.lower() == '.ipynb' def _render(self, file_pointer, **kwargs): content = file_pointer.read() nb = self._parse_json(content) name, theme = self._get_metadata(nb)
def read(self, filepath): metadata = {} # Files filedir = os.path.dirname(filepath) filename = os.path.basename(filepath) metadata_filename = filename.split(".")[0] + ".ipynb-meta" metadata_filepath = os.path.join(filedir, metadata_filename) # Load metadata if os.path.exists(metadata_filepath): # Metadata is on a external file, process using Pelican MD Reader md_reader = MarkdownReader(self.settings) _content, metadata = md_reader.read(metadata_filepath) else: # Load metadata from ipython notebook file ipynb_file = open(filepath) metadata = json.load(ipynb_file)["metadata"] # Fix metadata to pelican standards for key, value in metadata.items(): del metadata[key] key = key.lower() metadata[key] = self.process_metadata(key, value) metadata["ipython"] = True # Convert ipython notebook to html config = Config({"CSSHTMLHeaderTransformer": {"enabled": True, "highlight_class": ".highlight-ipynb"}}) exporter = HTMLExporter(config=config, template_file="basic", filters={"highlight2html": custom_highlighter}) content, info = exporter.from_filename(filepath) # Process using Pelican HTMLReader content = "<body>{0}</body>".format(content) # So Pelican HTMLReader works parser = MyHTMLParser(self.settings, filename) parser.feed(content) parser.close() body = parser.body summary = parser.summary metadata["summary"] = summary # Remove some CSS styles, so it doesn't break the themes. def filter_tags(style_text): style_list = style_text.split("\n") exclude = [ "p", "h1", "h2", "h3", "h4", "h5", "h6", "a", "ul", "ol", "li", ".rendered_html", "@media", ".navbar", "nav.navbar", ".navbar-text", "code", "pre", "div.text_cell_render", ] style_list = [i for i in style_list if len(list(filter(i.startswith, exclude))) == 0] ans = "\n".join(style_list) return '<style type="text/css">{0}</style>'.format(ans) css = "\n".join(filter_tags(css) for css in info["inlining"]["css"]) css = css + CUSTOM_CSS body = css + body return body, metadata
def main(): # command-line options define("debug", default=False, help="run in debug mode", type=bool) define("port", default=5000, help="run on the given port", type=int) define("cache_expiry_min", default=10*60, help="minimum cache expiry (seconds)", type=int) define("cache_expiry_max", default=2*60*60, help="maximum cache expiry (seconds)", type=int) define("mc_threads", default=1, help="number of threads to use for Async Memcache", type=int) define("threads", default=1, help="number of threads to use for background IO", type=int) tornado.options.parse_command_line() # NBConvert config config = Config() config.HTMLExporter.template_file = 'basic' config.NbconvertApp.fileext = 'html' config.CSSHTMLHeaderTransformer.enabled = False # don't strip the files prefix - we use it for redirects # config.Exporter.filters = {'strip_files_prefix': lambda s: s} exporter = HTMLExporter(config=config, log=log.app_log) # DEBUG env implies both autoreload and log-level if os.environ.get("DEBUG"): options.debug = True logging.getLogger().setLevel(logging.DEBUG) # setup memcache mc_pool = ThreadPoolExecutor(options.mc_threads) pool = ThreadPoolExecutor(options.threads) memcache_urls = os.environ.get('MEMCACHIER_SERVERS', os.environ.get('MEMCACHE_SERVERS') ) if pylibmc and memcache_urls: kwargs = dict(pool=mc_pool) username = os.environ.get('MEMCACHIER_USERNAME', '') password = os.environ.get('MEMCACHIER_PASSWORD', '') if username and password: kwargs['binary'] = True kwargs['username'] = username kwargs['password'] = password log.app_log.info("Using SASL memcache") else: log.app_log.info("Using plain memecache") cache = AsyncMultipartMemcache(memcache_urls.split(','), **kwargs) else: log.app_log.info("Using in-memory cache") cache = DummyAsyncCache() # setup tornado handlers and settings template_path = pjoin(here, 'templates') static_path = pjoin(here, 'static') env = Environment(loader=FileSystemLoader(template_path)) env.filters['markdown'] = markdown2html env.globals.update(nrhead=nrhead, nrfoot=nrfoot) AsyncHTTPClient.configure(HTTPClientClass) client = AsyncHTTPClient() github_client = AsyncGitHubClient(client) github_client.authenticate() settings = dict( jinja2_env=env, static_path=static_path, client=client, github_client=github_client, exporter=exporter, cache=cache, cache_expiry_min=options.cache_expiry_min, cache_expiry_max=options.cache_expiry_max, pool=pool, render_timeout=20, ) # create and start the app app = web.Application(handlers, debug=options.debug, **settings) http_server = httpserver.HTTPServer(app) log.app_log.info("Listening on port %i", options.port) http_server.listen(options.port) ioloop.IOLoop.instance().start()
def main(): # command-line options define("debug", default=False, help="run in debug mode", type=bool) define("no_cache", default=False, help="Do not cache results", type=bool) define("localfile", default=False, help="Allow to serve localfile under /localfile/* this can be a security risk", type=bool) define("localfile_path", default="", help="Allow to serve local files under relative or absolute path; this can be a security risk", type=str) define("localfile_uri", default="/localfile/", help="Allow to serve local files under url; this can be a security risk", type=str) define("port", default=5000, help="run on the given port", type=int) define("cache_expiry_min", default=10*60, help="minimum cache expiry (seconds)", type=int) define("cache_expiry_max", default=2*60*60, help="maximum cache expiry (seconds)", type=int) define("mc_threads", default=1, help="number of threads to use for Async Memcache", type=int) define("threads", default=1, help="number of threads to use for background IO", type=int) tornado.options.parse_command_line() # NBConvert config config = Config() config.HTMLExporter.template_file = 'basic' config.NbconvertApp.fileext = 'html' config.CSSHTMLHeaderTransformer.enabled = False # don't strip the files prefix - we use it for redirects # config.Exporter.filters = {'strip_files_prefix': lambda s: s} exporter = HTMLExporter(config=config, log=log.app_log) # DEBUG env implies both autoreload and log-level if os.environ.get("DEBUG"): options.debug = True logging.getLogger().setLevel(logging.DEBUG) # setup memcache mc_pool = ThreadPoolExecutor(options.mc_threads) pool = ThreadPoolExecutor(options.threads) memcache_urls = os.environ.get('MEMCACHIER_SERVERS', os.environ.get('MEMCACHE_SERVERS') ) if options.no_cache : log.app_log.info("Not using cache") cache = MockCache() elif pylibmc and memcache_urls: kwargs = dict(pool=mc_pool) username = os.environ.get('MEMCACHIER_USERNAME', '') password = os.environ.get('MEMCACHIER_PASSWORD', '') if username and password: kwargs['binary'] = True kwargs['username'] = username kwargs['password'] = password log.app_log.info("Using SASL memcache") else: log.app_log.info("Using plain memecache") cache = AsyncMultipartMemcache(memcache_urls.split(','), **kwargs) else: log.app_log.info("Using in-memory cache") cache = DummyAsyncCache() # setup tornado handlers and settings template_path = pjoin(here, 'templates') static_path = pjoin(here, 'static') env = Environment(loader=FileSystemLoader(template_path)) env.filters['markdown'] = markdown.markdown try: git_data = git_info(here) except Exception as e: app_log.error("Failed to get git info: %s", e) git_data = {} else: git_data['msg'] = escape(git_data['msg']) if options.no_cache : # force jinja to recompile template every time env.globals.update(cache_size=0) env.globals.update(nrhead=nrhead, nrfoot=nrfoot, git_data=git_data) AsyncHTTPClient.configure(HTTPClientClass) client = AsyncHTTPClient() github_client = AsyncGitHubClient(client) github_client.authenticate() settings = dict( log_function=log_request, jinja2_env=env, static_path=static_path, client=client, github_client=github_client, exporter=exporter, cache=cache, cache_expiry_min=options.cache_expiry_min, cache_expiry_max=options.cache_expiry_max, pool=pool, gzip=True, render_timeout=20, localfile_path=options.localfile_path, ) # create and start the app if options.localfile: log.app_log.warning("Serving local files, this can be a security risk") # use absolute or relative paths: handlers.insert(0, (r'%s(.*)' % options.localfile_uri, LocalFileHandler)) app = web.Application(handlers, debug=options.debug, **settings) http_server = httpserver.HTTPServer(app, xheaders=True) log.app_log.info("Listening on port %i", options.port) http_server.listen(options.port) ioloop.IOLoop.instance().start()
def read(self, filepath): metadata = {} # Files filedir = os.path.dirname(filepath) filename = os.path.basename(filepath) metadata_filename = filename.split('.')[0] + '.ipynb-meta' metadata_filepath = os.path.join(filedir, metadata_filename) # Load metadata if os.path.exists(metadata_filepath): # Metadata is on a external file, process using Pelican MD Reader md_reader = MarkdownReader(self.settings) _content, metadata = md_reader.read(metadata_filepath) else: # Load metadata from ipython notebook file ipynb_file = open(filepath) metadata = json.load(ipynb_file)['metadata'] # Fix metadata to pelican standards for key, value in metadata.items(): del metadata[key] key = key.lower() metadata[key] = self.process_metadata(key, value) metadata['ipython'] = True # Convert ipython notebook to html config = Config({'CSSHTMLHeaderTransformer': {'enabled': True, 'highlight_class': '.highlight-ipynb'}}) exporter = HTMLExporter(config=config, template_file='basic', filters={'highlight2html': custom_highlighter}) content, info = exporter.from_filename(filepath) if BeautifulSoup: soup = BeautifulSoup(content) for i in soup.findAll("div", {"class" : "input"}): if i.findChildren()[1].find(text='#ignore') is not None: i.extract() else: soup = content # Process using Pelican HTMLReader content = '<body>{0}</body>'.format(soup) # So Pelican HTMLReader works parser = MyHTMLParser(self.settings, filename) parser.feed(content) parser.close() body = parser.body summary = parser.summary metadata['summary'] = summary def filter_css(style_text): ''' HACK: IPython returns a lot of CSS including its own bootstrap. Get only the IPython Notebook CSS styles. ''' index = style_text.find('/*!\n*\n* IPython notebook\n*\n*/') if index > 0: style_text = style_text[index:] index = style_text.find('/*!\n*\n* IPython notebook webapp\n*\n*/') if index > 0: style_text = style_text[:index] style_text = re.sub(r'color\:\#0+(;)?', '', style_text) style_text = re.sub(r'\.rendered_html[a-z0-9 ]*\{[a-z0-9:;%.#\-\s\n]+\}', '', style_text) return '<style type=\"text/css\">{0}</style>'.format(style_text) ipython_css = '\n'.join(filter_css(css_style) for css_style in info['inlining']['css']) body = ipython_css + body + LATEX_CUSTOM_SCRIPT return body, metadata
def notebook(preprocessor, tag, markup): match = FORMAT.search(markup) if match: argdict = match.groupdict() src = argdict['src'] start = argdict['start'] end = argdict['end'] else: raise ValueError("Error processing input, " "expected syntax: {0}".format(SYNTAX)) if start: start = int(start) else: start = 0 if end: end = int(end) else: end = None settings = preprocessor.configs.config['settings'] nb_dir = settings.get('NOTEBOOK_DIR', 'notebooks') nb_path = os.path.join('content', nb_dir, src) if not os.path.exists(nb_path): raise ValueError("File {0} could not be found".format(nb_path)) # Create the custom notebook converter c = Config({ 'CSSHTMLHeaderTransformer': { 'enabled': True, 'highlight_class': '.highlight-ipynb' }, 'SubCell': { 'enabled': True, 'start': start, 'end': end } }) exporter = HTMLExporter(config=c, template_file='basic', filters={'highlight2html': custom_highlighter}, transformers=[SubCell], extra_loaders=[pelican_loader]) # read and parse the notebook with open(nb_path) as f: nb_text = f.read() nb_json = nbformat.reads_json(nb_text) (body, resources) = exporter.from_notebook_node(nb_json) # if we haven't already saved the header, save it here. if not notebook.header_saved: print("\n ** Writing styles to _nb_header.html: " "this should be included in the theme. **\n") header = '\n'.join( CSS_WRAPPER.format(css_line) for css_line in resources['inlining']['css']) header += JS_INCLUDE with open('_nb_header.html', 'w') as f: f.write(header) notebook.header_saved = True # this will stash special characters so that they won't be transformed # by subsequent processes. body = preprocessor.configs.htmlStash.store(body, safe=True) return body