def get(self): # get the organisms, maps, and models response = yield gen.Task(AsyncHTTPClient().fetch, get_url('server_index', protocol='http')) if response.code == 200 and response.body is not None: server_index_json = response.body.decode('utf-8') else: server_index_json = None # get the cached maps and models index = local_index() # render the template template = env.get_template('homepage.html') data = template.render(escher=get_url('escher_min', 'local'), homepage_css=get_url('homepage_css', 'local'), favicon=get_url('favicon', 'local'), logo=get_url('logo', 'local'), documentation=get_url('documentation', protocol='https'), github=get_url('github'), github_releases=get_url('github_releases'), homepage_js=get_url('homepage_js', 'local'), map_download_url=get_url('map_download', 'local'), server_index_json=b64dump(server_index_json), local_index_json=b64dump(index), version=__version__, web_version=False) self.set_header("Content-Type", "text/html") self.serve(data)
def save_html(self, filepath): """Save an HTML file containing the map. :param string filepath: The name of the HTML file. TODO apply options from self """ # options = transform(self.options) # get options options = {} for key in self.traits(option=True): val = getattr(self, key) if val is not None: options[key] = val options_json = json.dumps(options) template = env.get_template('standalone.html') embedded_css_b64 = (b64dump(self.embedded_css) if self.embedded_css is not None else None) html = template.render( escher_url=get_url('escher_min'), embedded_css_b64=embedded_css_b64, map_data_json_b64=b64dump(self._loaded_map_json), model_data_json_b64=b64dump(self._loaded_model_json), options_json_b64=b64dump(options_json), ) with open(expanduser(filepath), 'wb') as f: f.write(html.encode('utf-8'))
def test_b64dump(): assert b64decode(b64dump(None)) == 'null' accented_str = 'árvíztűrő tükörfúrógép' assert b64decode(b64dump(accented_str)) == accented_str obj = {'foo': 1, 'bar': 2} assert json.loads(b64decode(b64dump(obj))) == obj
def _get_html(self, js_source='web', menu='none', scroll_behavior='pan', html_wrapper=False, enable_editing=False, enable_keys=False, minified_js=True, fill_screen=False, height='800px', never_ask_before_quit=False, static_site_index_json=None, protocol=None, ignore_bootstrap=False): """Generate the Escher HTML. Arguments -------- js_source: Can be one of the following: 'web' - (Default) use js files from unpkg. 'local' - Use compiled js files in the local escher installation. Works offline. 'dev' - No longer necessary with source maps. This now gives the same behavior as 'local'. menu: Menu bar options include: 'none' - (Default) No menu or buttons. 'zoom' - Just zoom buttons (does not require bootstrap). 'all' - Menu and button bar (requires bootstrap). scroll_behavior: Scroll behavior options: 'pan' - (Default) Pan the map. 'zoom' - Zoom the map. 'none' - No scroll events. minified_js: If True, use the minified version of JavaScript files. html_wrapper: If True, return a standalone html file. enable_editing: Enable the editing modes (build, rotate, etc.). enable_keys: Enable keyboard shortcuts. height: The height of the HTML container. never_ask_before_quit: Never display an alert asking if you want to leave the page. By default, this message is displayed if enable_editing is True. static_site_index_json: The index, as a JSON string, for the static site. Use javascript to parse the URL options. Used for generating static pages (see static_site.py). protocol: The protocol can be 'http', 'https', or None which indicates a 'protocol relative URL', as in //escher.github.io. Ignored if source is local. ignore_bootstrap: Deprecated """ if js_source not in ['web', 'local', 'dev']: raise Exception('Bad value for js_source: %s' % js_source) if menu not in ['none', 'zoom', 'all']: raise Exception('Bad value for menu: %s' % menu) if scroll_behavior not in ['pan', 'zoom', 'none']: raise Exception('Bad value for scroll_behavior: %s' % scroll_behavior) content = env.get_template('content.html') # if height is not a string if type(height) is int: height = "%dpx" % height elif type(height) is float: height = "%fpx" % height elif type(height) is str: height = str(height) # set the proper urls url_source = 'local' if (js_source=='local' or js_source=='dev') else 'web' local_host = self.local_host # get the urls escher_url = get_url(('escher_min' if minified_js else 'escher'), url_source, local_host, protocol) favicon_url = get_url('favicon', url_source, local_host, protocol) # for static site map_download_url = get_url('map_download', url_source, local_host, protocol) model_download_url = get_url('model_download', url_source, local_host, protocol) # local host lh_string = ('' if local_host is None else local_host.rstrip('/') + '/') # options options = { 'menu': menu, 'enable_keys': enable_keys, 'enable_editing': enable_editing, 'scroll_behavior': scroll_behavior, 'fill_screen': fill_screen, 'never_ask_before_quit': never_ask_before_quit, 'reaction_data': self.reaction_data, 'metabolite_data': self.metabolite_data, 'gene_data': self.gene_data, } # Add the specified options for option in self.options: val = getattr(self, option) if val is None: continue options[option] = val html = content.render( # standalone title='Escher ' + ('Builder' if enable_editing else 'Viewer'), favicon_url=favicon_url, # content wrapper=html_wrapper, height=height, id=self.the_id, escher_url=escher_url, # dump json id_json=b64dump(self.the_id), options_json=b64dump(options), map_download_url_json=b64dump(map_download_url), model_download_url_json=b64dump(model_download_url), builder_embed_css_json=b64dump(self.embedded_css), # alreay json map_data_json=b64dump(self.loaded_map_json), model_data_json=b64dump(self.loaded_model_json), static_site_index_json=b64dump(static_site_index_json), ) return html