def prepare_environment(env=None): try: raise Exception() loader = PackageLoader("glycresoft_app", "html") loader.list_templates() except Exception: loader = FileSystemLoader( os.path.join(os.path.dirname(__file__), 'html')) if env is None: env = Environment(loader=loader) else: env.loader = loader env.fragment_cache = dict() env.filters["n_per_row"] = n_per_row env.filters['highlight_sequence_site'] = highlight_sequence_site env.filters['svg_plot'] = svg_plot env.filters['png_plot'] = png_plot env.filters['svguri_plot'] = svguri_plot env.filters['glycopeptide_string'] = glycopeptide_string env.filters['glycan_composition_string'] = glycan_composition_string env.filters["formula"] = formula env.filters["sort_peak_match_pairs"] = sort_peak_match_pairs env.filters["mass_charge_ratio"] = mass_charge_ratio env.filters["logit"] = logit return env
def prepare_environment(env=None): try: loader = PackageLoader("glycresoft_sqlalchemy.web_app", "html") loader.list_templates() except: loader = FileSystemLoader(os.path.join(os.path.dirname(__file__), 'html')) if env is None: env = Environment(loader=loader, extensions=[FragmentCacheExtension]) else: env.loader = loader env.add_extension(FragmentCacheExtension) env.fragment_cache = dict() env.filters["n_per_row"] = n_per_row env.filters['highlight_sequence_site'] = highlight_sequence_site env.filters['plot_glycoforms'] = plot_glycoforms env.filters['chromatogram'] = plot_chromatogram env.filters['svg_plot'] = svg_plot env.filters['png_plot'] = png_plot env.filters['fsort'] = fsort env.filters['glycopeptide_string'] = glycopeptide_string env.filters['glycan_composition_string'] = glycan_composition_string env.filters["glycopeptide_match_logo"] = glycopeptide_match_logo env.filters["formula"] = formula env.globals return env
def prepare_environment(env=None): try: loader = PackageLoader("glypy", "search/results_template") loader.list_templates() except: loader = FileSystemLoader(os.path.join(os.path.dirname(__file__), 'results_template')) if env is None: env = Environment(loader=loader, extensions=[FragmentCacheExtension]) else: env.loader = loader env.add_extension(FragmentCacheExtension) env.filters["collect_fragments"] = collect_fragments env.filters["all_matches"] = fetch_all_matches env.filters["strip_derivatize"] = strip_derivatize_glycoct env.filters["scientific_notation"] = scientific_notation env.filters["cfg_plot"] = cfg_plot env.filters["simple_cfg_plot"] = simple_plot env.filters["min"] = min env.filters["max"] = max env.filters["unique"] = unique env.filters["limit_sigfig"] = limit_sigfig env.filters['css_escape'] = css_escape env.filters['greek_fragment_name'] = greek_fragment_names env.fragment_cache = dict() return env
def test_pep_451_import_hook(): class ImportHook(importlib.abc.MetaPathFinder, importlib.abc.Loader): def find_spec(self, name, path=None, target=None): if name != "res": return None spec = importlib.machinery.PathFinder.find_spec(name) return importlib.util.spec_from_file_location( name, spec.origin, loader=self, submodule_search_locations=spec.submodule_search_locations, ) def create_module(self, spec): return None # default behaviour is fine def exec_module(self, module): return None # we need this to satisfy the interface, it's wrong # ensure we restore `sys.meta_path` after putting in our loader before = sys.meta_path[:] try: sys.meta_path.insert(0, ImportHook()) package_loader = PackageLoader("res") assert "test.html" in package_loader.list_templates() finally: sys.meta_path[:] = before
def gen_from_template(template, directory, defaults_path, no_prompt): """Generate the new file from a template.""" loader = PackageLoader("templar") template = osp.split(template)[-1] if template not in loader.list_templates(): loader = FileSystemLoader(directory) if template not in loader.list_templates(): raise RuntimeError("Template {} not found!".format(template)) env = Environment(loader=loader) output = env.get_template(template) defaults = load_defaults(defaults_path) try: defaults = defaults[template] except KeyError: defaults = defaults["DEFAULT"] parsed = env.parse(env.loader.get_source(env, template)[0]) variables = meta.find_undeclared_variables(parsed) kwargs = {} if no_prompt: for var in defaults: if var in defaults: default = defaults[var] else: default = "" kwargs[var] = default else: for var in variables: if var in defaults: default = defaults[var] prompt = var + " [{}]: ".format(default) else: prompt = var + ": " default = None resp = input(prompt).strip() if resp == "" and default is not None: resp = default kwargs[var] = resp result = output.render(**kwargs) print(result) return result
from jinja2 import Environment, PackageLoader import sys def generate_index_page(env): template = env.get_template('index_template.html') a = template.render(title='Waleed\'s website') #a = a.encode(sys.stdout.encoding, errors='replace') #print(type(a)) index_file = open('index.html', mode='w', encoding='utf8') index_file.writelines(a) index_file.flush() index_file.close() if __name__ == "__main__": print(sys.stdout.encoding) pack_load = PackageLoader('WebsiteGenerator', 'html') templates = pack_load.list_templates() # List all the available templates for t in templates: print(t) env = Environment(loader=pack_load) generate_index_page(env)