def output_blob(self): """Output to the template after all messages.""" if self.current_file_linted is None: # There were no files checked. print('[ERROR]: No files were checked.') return template_f = self.linter.config.pyta_template_file template = Environment(loader=FileSystemLoader(TEMPLATES_DIR)).get_template(template_f) # Embed resources so the output html can go anywhere, independent of assets. # with open(os.path.join(TEMPLATES_DIR, 'pyta_logo_markdown.png'), 'rb+') as image_file: # # Encode img binary to base64 (+33% size), decode to remove the "b'" # pyta_logo_base64_encoded = b64encode(image_file.read()).decode() # Date/time (24 hour time) format: # Generated: ShortDay. ShortMonth. PaddedDay LongYear, Hour:Min:Sec dt = str(datetime.now().strftime('%a. %b. %d %Y, %I:%M:%S %p')) output_path = os.path.join(os.getcwd(), self.linter.config.pyta_output_file) with open(output_path, 'w') as f: f.write(template.render(date_time=dt, # pyta_logo=pyta_logo_base64_encoded, reporter=self)) print('Opening your report in a browser...') output_url = 'file:///{}'.format(output_path) webbrowser.open(output_url, new=2)
def render_workflow_html_template(filename, subtemplate, filelists, **kwargs): """ Writes a template given inputs from the workflow generator. Takes a list of tuples. Each tuple is a pycbc File object. Also the name of the subtemplate to render and the filename of the output. """ dirnam = os.path.dirname(filename) makedir(dirnam) try: filenames = [f.name for filelist in filelists for f in filelist if f is not None] except TypeError: filenames = [] # render subtemplate subtemplate_dir = pycbc.results.__path__[0] + '/templates/wells' env = Environment(loader=FileSystemLoader(subtemplate_dir)) env.globals.update(get_embedded_config=get_embedded_config) env.globals.update(path_exists=os.path.exists) env.globals.update(len=len) subtemplate = env.get_template(subtemplate) context = {'filelists' : filelists, 'dir' : dirnam} context.update(kwargs) output = subtemplate.render(context) # save as html page kwds = {'render-function' : 'render_tmplt', 'filenames' : ','.join(filenames)} save_html_with_metadata(str(output), filename, None, kwds)
def render_text(path, cp): """ Render a file as text. """ # define filename and slug from path filename = os.path.basename(path) slug = filename.replace('.', '_') # initializations content = None # read file as a string with codecs.open(path, 'r', encoding='utf-8', errors='replace') as fp: content = fp.read() # replace all the escaped characters content = unescape(content, unescape_table) # render template template_dir = pycbc.results.__path__[0] + '/templates/files' env = Environment(loader=FileSystemLoader(template_dir)) env.globals.update(abs=abs) env.globals.update(path_exists=os.path.exists) template = env.get_template('file_pre.html') context = {'filename' : filename, 'slug' : slug, 'cp' : cp, 'content' : content} output = template.render(context) return output
def test_preserve_blocks(self): env = Environment(loader=DictLoader({ 'a': '{% if false %}{% block x %}A{% endblock %}{% endif %}{{ self.x() }}', 'b': '{% extends "a" %}{% block x %}B{{ super() }}{% endblock %}' })) tmpl = env.get_template('b') assert tmpl.render() == 'BA'
def render_tmplt(path, cp): """ Render a file as text. """ # define filename and slug from path filename = os.path.basename(path) slug = filename.replace('.', '_') # initializations content = None # read file as a string with open(path, 'rb') as fp: content = fp.read() # replace all the escaped characters content = unescape(content, unescape_table) # render template template_dir = '/'.join(path.split('/')[:-1]) env = Environment(loader=FileSystemLoader(template_dir)) env.globals.update(setup_template_render=setup_template_render) env.globals.update(get_embedded_config=get_embedded_config) env.globals.update(path_exists=os.path.exists) template = env.get_template(filename) context = {'filename' : filename, 'slug' : slug, 'cp' : cp, 'content' : content} output = template.render(context) return output
def render_all(template_path=None, output_path=None, static_path=None): if not template_path: template_path = 'lae_site/templates/' if not output_path: output_path = 'lae_rendered/' if not static_path: static_path = 'content/static/' env = Environment(loader=MyLoader(template_path)) # find all the files inside all the subdirectories of the template path all_the_things = os.walk(template_path) for root, dirs, files in all_the_things: for f in files: # check that it's a template file if f[-5:] == '.html' and f[:1] != '_': full_path = root + '/' + f # path relative to template_path relative_path = full_path[len(template_path) + 1:] print "Rendering " + relative_path # render the template template = env.get_template(relative_path) # calculate directory output should go in dirname = os.path.dirname(output_path + '/' + relative_path) # and if it doesn't exist yet, create it if not os.path.exists(dirname): os.makedirs(dirname) # make rendered html file with codecs.open(output_path + '/' + relative_path, 'w', 'utf-8') as render_file: for line in template.render(): render_file.write(line) if not os.path.isdir(output_path + 'static'): subprocess.check_call(['ln', '-s', '../' + static_path, 'static'], cwd=os.getcwd() + '/' + output_path) print "Made symlink to static files." subprocess.check_call(['python', '-m', 'SimpleHTTPServer', '8002'], cwd=os.getcwd() + '/' + output_path)
def _get_environment(): """Get the Jinja2 environment :returns: the jinja2 environment """ global environment if environment is None: import jip cfg = jip.config.templates # global environment environment = Environment(undefined=JipUndefined, variable_start_string=cfg.get( 'variable_open', '${' ), variable_end_string=cfg.get( 'variable_close', '}' )) environment.filters['arg'] = arg_filter environment.filters['else'] = else_filter environment.filters['name'] = name_filter environment.filters['ext'] = ext_filter environment.filters['abs'] = abs_filter environment.filters['suf'] = suf_filter environment.filters['pre'] = pre_filter environment.filters['parent'] = parent_filter environment.filters['re'] = replace_filter return environment
def init_jinja2(app, **kw): logging.info('init jinja2...') #初始化模板配置,包括模板运行代码的开始结束标识符,变量的开始结束标识符等 options = dict( autoescape = kw.get('autoescape', True), #是否转义设置为True,就是在渲染模板时自动把变量中的<>&等字符转换为<>& block_start_string = kw.get('block_start_string', '{%'), #运行代码的开始标识符 block_end_string = kw.get('block_end_string', '%}'), #运行代码的结束标识符 variable_start_string = kw.get('variable_start_string', '{{'), #变量开始标识符 variable_end_string = kw.get('variable_end_string', '}}'), #变量结束标识符 auto_reload = kw.get('auto_reload', True) #Jinja2会在使用Template时检查模板文件的状态,如果模板有修改, 则重新加载模板。如果对性能要求较高,可以将此值设为False ) #从参数中获取path字段,即模板文件的位置 path = kw.get('path', None) #如果没有,则默认为当前文件目录下的 templates 目录 if path is None: path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates') logging.info('set jinja2 template path: %s' % path) #Environment是Jinja2中的一个核心类,它的实例用来保存配置、全局对象,以及从本地文件系统或其它位置加载模板。 #这里把要加载的模板和配置传给Environment,生成Environment实例 env = Environment(loader=FileSystemLoader(path), **options) #从参数取filter字段 # filters: 一个字典描述的filters过滤器集合, 如果非模板被加载的时候, 可以安全的添加filters或移除较早的. filters = kw.get('filters', None) #如果有传入的过滤器设置,则设置为env的过滤器集合 if filters is not None: for name, f in filters.items(): env.filters[name] = f #给webapp设置模板 app['__templating__'] = env
def make_exercise(src, tgt): problems = [] exercise = json.load(open(src)) problems = [] for problem in expand_problems(exercise.pop('problems')): problem['query'] = query = " ".join([problem['instruction'], problem['latex']]) problem['exid'] = exid = "SYMB_" + hashlib.md5(query).hexdigest().upper()[:7] solution = get_solution(query) for k in "computesTo debugInfo".split(): solution.pop(k, None) problem['solution'] = solution.pop('solutions')[0] problem.setdefault('title', exercise['default_problem_title']) problem.setdefault('latex_buttons', exercise['latex_buttons']) problems.append(problem) exercise['problems'] = problems from jinja2 import Environment, FileSystemLoader env = Environment(trim_blocks=True, lstrip_blocks=True, loader=FileSystemLoader(BASE_PATH)) env.filters.update(latex=latex, direction=direction) template_name = exercise.get("template", "template.html") template = env.get_template(template_name) html = template.render(exercise=exercise) if obfuscate: html = "".join(html.splitlines()) with open(tgt, "w") as f: print("Writing %s" % tgt) print(html.encode("utf8"), file=f) for problem in problems: print('%(host)s/exercises/%(url)s?problem=%(exid)s\n\t%(query)s\n' % dict(problem, host=LOCAL_HOST, url=basename(tgt))) print("- Done (%s problems)" % len(problems))
def test_volatile(self): env = Environment(extensions=['jinja2.ext.autoescape'], autoescape=True) tmpl = env.from_string('{% autoescape foo %}{{ {"foo": "<test>"}' '|xmlattr|escape }}{% endautoescape %}') assert tmpl.render(foo=False) == ' foo="&lt;test&gt;"' assert tmpl.render(foo=True) == ' foo="<test>"'
def test_nested_structures(self): env = Environment(extensions=[SerializerExtension]) rendered = env.from_string('{{ data }}').render(data="foo") self.assertEqual(rendered, u"foo") data = OrderedDict([ ('foo', OrderedDict([ ('bar', 'baz'), ('qux', 42) ]) ) ]) rendered = env.from_string('{{ data }}').render(data=data) self.assertEqual(rendered, u"{'foo': {'bar': 'baz', 'qux': 42}}") rendered = env.from_string('{{ data }}').render(data=[ OrderedDict( foo='bar', ), OrderedDict( baz=42, ) ]) self.assertEqual(rendered, u"[{'foo': 'bar'}, {'baz': 42}]")
def build(outfile, template, content_file): content = Content(content_file) template_dir, template_file = os.path.split(template) output_dir, output_file = os.path.split(outfile) env = Environment( block_start_string="%{", block_end_string="%}", variable_start_string="%{{", variable_end_string="%}}", comment_start_string="%{#", comment_end_string="%#}", line_comment_prefix="%#", line_statement_prefix="%##", loader=FileSystemLoader(template_dir), ) template = env.get_template(template_file) with codecs.open(outfile, "wb", encoding="utf-8") as f: f.write( template.render( last_updated=format_date(), git_hash=content.get_git_hash(), schools=content.get_schools(), experience=content.get_experience(), skills=content.get_skills(), languages=content.get_languages(), ) ) os.system('lualatex -shell-escape --output-directory="' + output_dir + '" ' + outfile)
def render(db): env = Environment(loader=FileSystemLoader(template_dir)) env.filters['toUrl'] = toUrl # Copy resources shutil.copytree(template_dir + 'assets', destination_dir + 'assets') shutil.copytree('build/avatars', destination_dir + 'avatars') # Profile pages councillors = db['councillors'] votes_mapping = db['votes'].find() votes_mapping = {curr["_id"]: curr for curr in votes_mapping} # for m in votes_mapping: # print(m) # print(votes_mapping) for councillor in councillors.find(): name = 'grossraete/{0}/index'.format(toUrl(councillor['fullname'])) render_page(env, 'councillor_details', name, councillor=councillor, votes_mapping=votes_mapping, title=councillor['fullname']) # Index pages render_page(env, 'councillor_index', 'grossraete/index', db=db, councillors=councillors.find(), title='Grossräte') # Base for comparison render_page(env, 'compare', 'vergleichen/index')
def render_all(template_path=None, output_path=None): if not template_path: template_path = os.getcwd() + '/templates' if not output_path: output_path = os.getcwd() + '/rendered_website' env = Environment(loader=MyLoader(template_path)) # find all the files inside all the subdirectories of the template path all_the_things = os.walk(template_path) for root, dirs, files in all_the_things: for f in files: # check that it's a template file if f[-5:] == '.html' and f[:1] != '_': full_path = root + '/' + f # path relative to template_path relative_path = full_path[len(template_path) + 1:] print "Rendering " + relative_path # render the template template = env.get_template(relative_path) # calculate directory output should go in dirname = os.path.dirname(output_path + '/' + relative_path) # and if it doesn't exist yet, create it if not os.path.exists(dirname): os.makedirs(dirname) # make rendered html file with codecs.open(output_path + '/' + relative_path, 'w', 'utf-8') as render_file: for line in template.render(): render_file.write(line)
def generate_html(self): self.set_baselineinfo() self.analyze_result() self.copy_template() env = Environment(loader=PackageLoader('ChorusCore', 'templates')) for suite_name, suiteresult in self.result.suites.iteritems(): suitetemplate = env.get_template("suite.html") content = suitetemplate.render({"result":suiteresult.__dict__, "bsinfo":self.updateinfo}) filename = os.path.join(self.output_path, '%s.html' % suite_name ) Utils.write_to_file(filename, content, "w+") self.logger.info("Suite %s.html generated" % suite_name) summarytemplate = env.get_template('summary.html') xmltemplate = env.get_template('summary.xml') content = summarytemplate.render({"result":self.result,"ea":Performance_Result}) filename = os.path.join(self.output_path, 'Summary.html') Utils.write_to_file(filename, content, "w+") self.logger.info("Summary.html generated") #TODO Modify xml content = xmltemplate.render({"result":self.result}) filename = os.path.join(self.output_path, 'Summary.xml') Utils.write_to_file(filename, content, "w+") self.logger.info("Summary.xml generated") if os.environ.has_key("BUILD_URL"): self.generate_result_email() if Performance_Result.data: self.generate_performance_result()
class jinja2_renderer_factory(object): def __init__(self, search_paths=(), default_templates='deform_jinja2:templates', translator=None, extensions=[]): if 'jinja2.ext.i18n' not in extensions: extensions.append('jinja2.ext.i18n') self.env = Environment(extensions=extensions) self.env.loader = FileSystemLoader(()) for path in search_paths: self.add_search_path(path) if translator == None: translator = DummyTranslator self.env.install_gettext_callables(translator.gettext, translator.ngettext) self.add_search_path(default_templates) def add_search_path(self, path): self.env.loader.searchpath.append(resource_filename(*(path.split(':')))) def add_filter(self, name, func): self.env.filters[name] = func def __call__(self, tname, **kw): if not '.jinja2' in tname: tname += '.jinja2' template = self.env.get_template(tname) return template.render(**kw)
def init_jinja2(app, **kw): logging.info("init jinja2...") # 初始化模板配置,包括模板运行代码的开始结束标识符,变量的开始结束标识符等 options = dict( # 是否转义设置为True,就是在渲染模板时自动把变量中的<>&等字符转换为<>& autoescape = kw.get("autoescape", True), # 自动转义xml/html的特殊字符 block_start_string = kw.get("block_start_string", "{%"), # 代码块开始标志 block_end_string = kw.get("block_end_string", "%}"), # 代码块结束标志 variable_start_string = kw.get("variable_start_string", "{{"), # 变量开始标志 variable_end_string = kw.get("variable_end_string", "}}"), # 变量结束标志 # Jinja2会在使用Template时检查模板文件的状态,如果模板有修改, 则重新加载模板。如果对性能要求较高,可以将此值设为False auto_reload = kw.get("auto_reload", True) # 每当对模板发起请求,加载器首先检查模板是否发生改变.若是,则重载模板 ) # 从参数中获取path字段,即模板文件的位置 path = kw.get("path", None) if path is None: # 若路径不存在,则默认为当前文件目录下的 templates 目录 # os.path.abspath(__file__), 返回当前脚本的绝对路径(包括文件名) # os.path.dirname(), 去掉文件名,返回目录路径 # os.path.join(), 将分离的各部分组合成一个路径名 path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates") logging.info("set jinja2 template path: %s" % path) # Environment是Jinja2中的一个核心类,它的实例用来保存配置、全局对象,以及从本地文件系统或其它位置加载模板。 # 这里把要加载的模板和配置传给Environment,生成Environment实例 # 加载器负责从指定位置加载模板, 此处选择FileSystemLoader,顾名思义就是从文件系统加载模板,前面我们已经设置了path env = Environment(loader = FileSystemLoader(path), **options) # 从参数取filter字段 # filters: 一个字典描述的filters过滤器集合, 如果非模板被加载的时候, 可以安全的添加filters或移除较早的. filters = kw.get("filters", None) # 如果有传入的过滤器设置,则设置为env的过滤器集合 if filters is not None: for name, f in filters.items(): env.filters[name] = f # 给webapp设置模板 app["__templating__"] = env
def test_prefix_loader(self): env = Environment(loader=prefix_loader) tmpl = env.get_template('a/test.html') assert tmpl.render().strip() == 'BAR' tmpl = env.get_template('b/justdict.html') assert tmpl.render().strip() == 'FOO' self.assert_raises(TemplateNotFound, env.get_template, 'missing')
def execute_programmatically(uid, pipeline, output_dir, notebook=None, script=None, **args): if isinstance(uid, str): # force it to look like a string in the templated notebook uid = "'{}'".format(uid) if notebook: template_dir = notebooks_path suffix = '.ipynb' elif script: template_dir = scripts_path suffix = '.py' else: raise ValueError("You must supply `notebook` or `script` as a kwarg") logger.info("Template directory = %s" % template_dir) jinja_env = Environment(loader=FileSystemLoader(template_dir)) output_path = os.path.join(output_dir, pipeline+suffix) logger.info("Writing to %s" % output_path) template_name = '%s.tmpl' % pipeline logger.info("Loading template = %s" % template_name) template = jinja_env.get_template(template_name) template_info = {'uid': uid} pipeline = template.render(**template_info) with open(output_path, 'w') as f: f.write(pipeline)
def test_filesystem_loader(self): env = Environment(loader=filesystem_loader) tmpl = env.get_template('test.html') assert tmpl.render().strip() == 'BAR' tmpl = env.get_template('foo/test.html') assert tmpl.render().strip() == 'FOO' self.assert_raises(TemplateNotFound, env.get_template, 'missing.html')
def test_choice_loader(self): env = Environment(loader=choice_loader) tmpl = env.get_template('justdict.html') assert tmpl.render().strip() == 'FOO' tmpl = env.get_template('test.html') assert tmpl.render().strip() == 'BAR' self.assert_raises(TemplateNotFound, env.get_template, 'missing.html')
class Jinja2Template(BaseTemplate): env = None # hopefully, a Jinja environment is actually thread-safe def prepare(self): if not self.env: from jinja2 import Environment, FunctionLoader self.env = Environment(line_statement_prefix="#", loader=FunctionLoader(self.loader)) if self.template: self.tpl = self.env.from_string(self.template) else: self.tpl = self.env.get_template(self.filename) def render(self, **args): return self.tpl.render(**args).encode("utf-8") def loader(self, name): if not name.endswith(".tpl"): for path in self.lookup: fpath = os.path.join(path, name+'.tpl') if os.path.isfile(fpath): name = fpath break f = open(name) try: return f.read() finally: f.close()
def _create_environment(self): """ Create the Jinja templating environment. """ from jinja2 import Environment, ChoiceLoader, FileSystemLoader here = os.path.dirname(os.path.realpath(__file__)) paths = self.template_path + \ [os.path.join(here, self.default_template_path), os.path.join(here, self.template_skeleton_path)] loaders = self.extra_loaders + [FileSystemLoader(paths)] environment = Environment( loader= ChoiceLoader(loaders), extensions=JINJA_EXTENSIONS ) environment.globals['uuid4'] = uuid.uuid4 # Add default filters to the Jinja2 environment for key, value in self.default_filters(): self._register_filter(environment, key, value) # Load user filters. Overwrite existing filters if need be. if self.filters: for key, user_filter in self.filters.items(): self._register_filter(environment, key, user_filter) return environment
def test_stacked_locals_scoping_bug(self): env = Environment(line_statement_prefix="#") t = env.from_string( """\ # for j in [1, 2]: # set x = 1 # for i in [1, 2]: # print x # if i % 2 == 0: # set x = x + 1 # endif # endfor # endfor # if a # print 'A' # elif b # print 'B' # elif c == d # print 'C' # else # print 'D' # endif """ ) assert t.render(a=0, b=False, c=42, d=42.0) == "1111C"
def main(): """ Get hardware info, format it, and write to slurm.conf and gres.conf """ args = parse_args() # Get info from cnode and xthwinv repurposed = get_repurposed_computes(args.partition) nodes = get_inventory(args.partition, repurposed) nodelist = rli_compress([int(nid) for nid in nodes]) compact_nodes(nodes) defmem, maxmem = get_mem_per_cpu(nodes) # Write files from templates jinjaenv = Environment(loader=FileSystemLoader(args.templatedir)) conffile = os.path.join(args.output, 'slurm.conf') print 'Writing Slurm configuration to {0}...'.format(conffile) with open(conffile, 'w') as outfile: outfile.write(jinjaenv.get_template('slurm.conf.j2').render( script=sys.argv[0], date=time.asctime(), controlmachine=args.controlmachine, grestypes=get_gres_types(nodes), defmem=defmem, maxmem=maxmem, nodes=nodes, nodelist=nodelist)) gresfilename = os.path.join(args.output, 'gres.conf') print 'Writing gres configuration to {0}...'.format(gresfilename) with open(gresfilename, 'w') as gresfile: gresfile.write(jinjaenv.get_template('gres.conf.j2').render( script=sys.argv[0], date=time.asctime(), nodes=nodes)) print 'Done.'
class Builder: def __init__(self, out_file): self.env = Environment(loader=PackageLoader('builder', 'templates')) self.out_file = out_file # provides a template helper to turn a list into a list of pairs self.env.filters['pairs'] = lambda l: [l[i:i + 2] for i in range(0, len(l), 2)] # inlines css using premailer with my specific configuration, namely, must # keep classes so responsiveness can work. def premailer(self, html): return Premailer(html, base_url=None, remove_classes=False).transform() # creates a string of rendered html def render(self, elements): body = '' # loop through given elements and render respective template with options for element in elements: template = self.env.get_template(element['template']) # gets the template body += template.render(element['options']) # renders the template layout = self.env.get_template('body.html') # get layout template output = layout.render(body=body) # render into layout return self.premailer(output) # writes the rendered string to the specified file def write(self, elements): output = self.render(elements) # builds the html string with open(self.out_file, "w") as html: # writes the html to a file html.write(output)
def to_html(self): from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader('%s/templates' % (os.path.dirname(os.path.abspath(__file__)),))) template = env.get_template('sql_table.html') html = template.render({"header": self.theader_list, "body": self.data}) return html
class RenderTestCase(TestCase): templates_path = os.path.join(os.path.dirname(os.getcwd()), u'templates') def setUp(self): self.env = Environment(loader=FileSystemLoader(self.templates_path)) def test_simple_list(self): dut = Element(u'ul', u'text', {u'class': u'some class'}) dut.child.append(Element(u'li', u'text', {u'class': u'some class'})) dut.child.append(Element(u'li', u'text', {u'class': u'some class'})) result = self.env.get_template(u'json2html.html').render(root=dut.render()) expect = u'<ul class="some class">text<li class="some class">text</li><li class="some class">text</li></ul>' self.assertEqual(result, expect) def test_property(self): dut = Element(u'ul', u'text', {u'class': u'some class'}) dut.child.append(Element(u'li', u'text', {u'disable': None})) dut.child.append(Element(u'li', u'text', {u'disable': u''})) result = self.env.get_template(u'json2html.html').render(root=dut.render()) expect = u'<ul class="some class">text<li disable>text</li><li disable>text</li></ul>' self.assertEqual(result, expect)
def fill_template(player_data, lang, fundamental, yaku): """Build long text which shows the statistics of the target player(s). """ target_games = player_data[0]['games'] if player_data else None if not target_games: return 'NO DATA\n' env = Environment(autoescape=False) env.filters.update( format_float=format_float, format_percentage=format_percentage,) output_text = env.from_string(lang.tmpl_summary).render( count_games=len(target_games), started_at=target_games[0]['started_at'], finished_at=target_games[-1]['finished_at'], data=player_data) if fundamental: output_text += env.from_string(lang.tmpl_fundamental).render( data=player_data) if yaku: yaku_name_map = {y:lang.yaku_names[i] for i, y in enumerate(YakuTable)} output_text += env.from_string(lang.tmpl_yaku_freq).render( data=player_data, YakuTable=YakuTable, yaku_name_map=yaku_name_map) return output_text
def warning(message): """Print a warning message on the rich text view""" env = Environment() env.loader = FileSystemLoader(osp.join(CONFDIR_PATH, 'templates')) warning = env.get_template("warning.html") return warning.render(css_path=CSS_PATH, text=message)
TxStates, TxTypes, strOfferState, strBidState, strTxState, getLockName, SEQUENCE_LOCK_TIME, ABS_LOCK_TIME, ) def format_timestamp(value): return time.strftime('%Y-%m-%d %H:%M', time.localtime(value)) env = Environment(loader=PackageLoader('basicswap', 'templates')) env.filters['formatts'] = format_timestamp PAGE_LIMIT = 50 def getCoinName(c): return chainparams[c]['name'].capitalize() def listAvailableCoins(swap_client): coins = [] for k, v in swap_client.coin_clients.items(): if v['connection_type'] == 'rpc': coins.append((int(k), getCoinName(k))) return coins
def test_none_gives_proper_error(self): try: Environment().getattr(None, 'split')() except UndefinedError, e: assert e.message == "'None' has no attribute 'split'"
:copyright: (c) 2010 by the Jinja Team. :license: BSD, see LICENSE for more details. """ import os import time import tempfile import unittest from jinja2.testsuite import JinjaTestCase from jinja2 import Environment, Undefined, DebugUndefined, \ StrictUndefined, UndefinedError, Template, meta, \ is_undefined, Template, DictLoader from jinja2.utils import Cycler env = Environment() class ExtendedAPITestCase(JinjaTestCase): def test_item_and_attribute(self): from jinja2.sandbox import SandboxedEnvironment for env in Environment(), SandboxedEnvironment(): # the |list is necessary for python3 tmpl = env.from_string('{{ foo.items()|list }}') assert tmpl.render(foo={'items': 42}) == "[('items', 42)]" tmpl = env.from_string('{{ foo|attr("items")()|list }}') assert tmpl.render(foo={'items': 42}) == "[('items', 42)]" tmpl = env.from_string('{{ foo["items"] }}') assert tmpl.render(foo={'items': 42}) == '42'
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import funct import sql import http.cookies from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader('templates/'), autoescape=True) template = env.get_template('add.html') form = funct.form serv = form.getvalue('serv') if form.getvalue('add'): c = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) c["restart"] = form.getvalue('serv') print(c) print('Content-type: text/html\n') funct.check_login() funct.page_for_admin(level=2) try: user, user_id, role, token, servers = funct.get_users_params() cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) group = cookie.get('group') user_group = group.value except: pass dir = os.path.dirname(os.getcwd()) + "/" + sql.get_setting('lists_path') white_dir = os.path.dirname(os.getcwd()) + "/" + sql.get_setting(
from ncclient import manager from jinja2 import Environment from jinja2 import FileSystemLoader router = { 'host': 'ios-xe-mgmt-latest.cisco.com', 'port': '10000', 'username': '******', 'password': '******' } my_template = Environment(loader=FileSystemLoader('templates')) template_vars = {"description": "Changed again"} template = my_template.get_template("interface.j2.xml") netconf_payload = template.render(template_vars) # Alternative: it can also be a little easier. # description = "Changed description" # template = my_template.get_template("interface.j2.xml") # netconf_payload = template.render(description=description) print(netconf_payload) m = manager.connect(host=router['host'], port=router['port'], username=router['username'], password=router['password'], device_params={'name': 'iosxe'}, hostkey_verify=False)
def __init__(self): self.env = Environment(loader=FileSystemLoader(searchpath=os.path.dirname(__file__))) self.base_template = self.env.get_template('report_en_v2.html')
class ReportGenerator: """ """ def __init__(self): self.env = Environment(loader=FileSystemLoader(searchpath=os.path.dirname(__file__))) self.base_template = self.env.get_template('report_en_v2.html') def write_report_to_file(self, report_params): """ Args: report_params: Returns: """ with open(os.path.join(report_params['output_path'], 'autowoe_report.html'), "w", encoding='utf-8') as f: f.write(self.base_template.render( report_name=str(report_params['report_name']), report_version=str(report_params['report_version_id']), city=str(report_params['city']), year=str(datetime.now().year), model_aim=str(report_params['model_aim']), model_name=str(report_params['model_name']), zakazchik=str(report_params['zakazchik']), high_level_department=str(report_params['high_level_department']), ds_name=str(report_params['ds_name']), target_descr=str(report_params["target_descr"]), non_target_descr=str(report_params["non_target_descr"]), count_train=report_params["count_train"], train_target_cnt=report_params["train_target_cnt"], train_nontarget_cnt=report_params["train_nontarget_cnt"], train_target_perc=report_params["train_target_perc"], train_auc_full=report_params["train_auc_full"], train_gini_full=report_params["train_gini_full"], count_test=report_params["count_test"], test_target_cnt=report_params["test_target_cnt"], test_nontarget_cnt=report_params["test_nontarget_cnt"], test_target_perc=report_params["test_target_perc"], test_auc_full=report_params["test_auc_full"], test_gini_full=report_params["test_gini_full"], train_gini_confint=report_params["train_gini_confint"], test_gini_confint=report_params["test_gini_confint"], model_coef=report_params["model_coef"], p_vals=report_params["p_vals"], p_vals_test=report_params["p_vals_test"], final_nan_stat=report_params["final_nan_stat"], features_roc_auc=report_params["features_roc_auc"], features_woe=report_params["features_woe"], woe_bars=report_params["woe_bars"], backlash_plots=report_params["backlash_plots"], train_vif=report_params["train_vif"], psi_total=report_params["psi_total"], psi_zeros=report_params["psi_zeros"], psi_ones=report_params["psi_ones"], psi_binned_total=report_params["psi_binned_total"], psi_binned_zeros=report_params["psi_binned_zeros"], psi_binned_ones=report_params["psi_binned_ones"], scorecard=report_params["scorecard"], feature_history=report_params["feature_history"], feature_contribution=report_params["feature_contribution"], corr_map_table=report_params["corr_map_table"], binned_p_stats_train=report_params["binned_p_stats_train"], binned_p_stats_test=report_params["binned_p_stats_test"], dategrouped_value=report_params["dategrouped_value"], dategrouped_gini=report_params["dategrouped_gini"], dategrouped_nan=report_params["dategrouped_nan"], )) def generate_report(self, report_params): """ Args: report_params: Returns: """ copyfile(os.path.join(os.path.dirname(__file__), 'shaptxt'), os.path.join(report_params['output_path'], 'shap.js')) self.write_report_to_file(report_params) logger.info('Successfully wrote {}.'.format(os.path.join(report_params['output_path'], 'autowoe_report.html')))
class Styler(object): """ Helps style a DataFrame or Series according to the data with HTML and CSS. Parameters ---------- data: Series or DataFrame precision: int precision to round floats to, defaults to pd.options.display.precision table_styles: list-like, default None list of {selector: (attr, value)} dicts; see Notes uuid: str, default None a unique identifier to avoid CSS collisons; generated automatically caption: str, default None caption to attach to the table Attributes ---------- env : Jinja2 Environment template : Jinja2 Template loader : Jinja2 Loader Notes ----- Most styling will be done by passing style functions into ``Styler.apply`` or ``Styler.applymap``. Style functions should return values with strings containing CSS ``'attr: value'`` that will be applied to the indicated cells. If using in the Jupyter notebook, Styler has defined a ``_repr_html_`` to automatically render itself. Otherwise call Styler.render to get the genterated HTML. CSS classes are attached to the generated HTML * Index and Column names include ``index_name`` and ``level<k>`` where `k` is its level in a MultiIndex * Index label cells include * ``row_heading`` * ``row<n>`` where `n` is the numeric position of the row * ``level<k>`` where `k` is the level in a MultiIndex * Column label cells include * ``col_heading`` * ``col<n>`` where `n` is the numeric position of the column * ``evel<k>`` where `k` is the level in a MultiIndex * Blank cells include ``blank`` * Data cells include ``data`` See Also -------- pandas.DataFrame.style """ loader = PackageLoader("pandas", "io/formats/templates") env = Environment( loader=loader, trim_blocks=True, ) template = env.get_template("html.tpl") def __init__(self, data, precision=None, table_styles=None, uuid=None, caption=None, table_attributes=None): self.ctx = defaultdict(list) self._todo = [] if not isinstance(data, (pd.Series, pd.DataFrame)): raise TypeError("``data`` must be a Series or DataFrame") if data.ndim == 1: data = data.to_frame() if not data.index.is_unique or not data.columns.is_unique: raise ValueError("style is not supported for non-unique indicies.") self.data = data self.index = data.index self.columns = data.columns self.uuid = uuid self.table_styles = table_styles self.caption = caption if precision is None: precision = get_option('display.precision') self.precision = precision self.table_attributes = table_attributes # display_funcs maps (row, col) -> formatting function def default_display_func(x): if is_float(x): return '{:>.{precision}g}'.format(x, precision=self.precision) else: return x self._display_funcs = defaultdict(lambda: default_display_func) def _repr_html_(self): """Hooks into Jupyter notebook rich display system.""" return self.render() @Appender(_shared_docs['to_excel'] % dict(axes='index, columns', klass='Styler', axes_single_arg="{0 or 'index', 1 or 'columns'}", optional_by=""" by : str or list of str Name or list of names which refer to the axis items.""", versionadded_to_excel='\n .. versionadded:: 0.20')) def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, startrow=0, startcol=0, engine=None, merge_cells=True, encoding=None, inf_rep='inf', verbose=True, freeze_panes=None): from pandas.io.formats.excel import ExcelFormatter formatter = ExcelFormatter(self, na_rep=na_rep, cols=columns, header=header, float_format=float_format, index=index, index_label=index_label, merge_cells=merge_cells, inf_rep=inf_rep) formatter.write(excel_writer, sheet_name=sheet_name, startrow=startrow, startcol=startcol, freeze_panes=freeze_panes, engine=engine) def _translate(self): """ Convert the DataFrame in `self.data` and the attrs from `_build_styles` into a dictionary of {head, body, uuid, cellstyle} """ table_styles = self.table_styles or [] caption = self.caption ctx = self.ctx precision = self.precision uuid = self.uuid or str(uuid1()).replace("-", "_") ROW_HEADING_CLASS = "row_heading" COL_HEADING_CLASS = "col_heading" INDEX_NAME_CLASS = "index_name" DATA_CLASS = "data" BLANK_CLASS = "blank" BLANK_VALUE = "" def format_attr(pair): return "{key}={value}".format(**pair) # for sparsifying a MultiIndex idx_lengths = _get_level_lengths(self.index) col_lengths = _get_level_lengths(self.columns) cell_context = dict() n_rlvls = self.data.index.nlevels n_clvls = self.data.columns.nlevels rlabels = self.data.index.tolist() clabels = self.data.columns.tolist() if n_rlvls == 1: rlabels = [[x] for x in rlabels] if n_clvls == 1: clabels = [[x] for x in clabels] clabels = list(zip(*clabels)) cellstyle = [] head = [] for r in range(n_clvls): # Blank for Index columns... row_es = [{ "type": "th", "value": BLANK_VALUE, "display_value": BLANK_VALUE, "is_visible": True, "class": " ".join([BLANK_CLASS]) }] * (n_rlvls - 1) # ... except maybe the last for columns.names name = self.data.columns.names[r] cs = [ BLANK_CLASS if name is None else INDEX_NAME_CLASS, "level{lvl}".format(lvl=r) ] name = BLANK_VALUE if name is None else name row_es.append({ "type": "th", "value": name, "display_value": name, "class": " ".join(cs), "is_visible": True }) if clabels: for c, value in enumerate(clabels[r]): cs = [ COL_HEADING_CLASS, "level{lvl}".format(lvl=r), "col{col}".format(col=c) ] cs.extend( cell_context.get("col_headings", {}).get(r, {}).get(c, [])) es = { "type": "th", "value": value, "display_value": value, "class": " ".join(cs), "is_visible": _is_visible(c, r, col_lengths), } colspan = col_lengths.get((r, c), 0) if colspan > 1: es["attributes"] = [ format_attr({ "key": "colspan", "value": colspan }) ] row_es.append(es) head.append(row_es) if self.data.index.names and _any_not_none(*self.data.index.names): index_header_row = [] for c, name in enumerate(self.data.index.names): cs = [INDEX_NAME_CLASS, "level{lvl}".format(lvl=c)] name = '' if name is None else name index_header_row.append({ "type": "th", "value": name, "class": " ".join(cs) }) index_header_row.extend([{ "type": "th", "value": BLANK_VALUE, "class": " ".join([BLANK_CLASS]) }] * len(clabels[0])) head.append(index_header_row) body = [] for r, idx in enumerate(self.data.index): row_es = [] for c, value in enumerate(rlabels[r]): rid = [ ROW_HEADING_CLASS, "level{lvl}".format(lvl=c), "row{row}".format(row=r) ] es = { "type": "th", "is_visible": _is_visible(r, c, idx_lengths), "value": value, "display_value": value, "id": "_".join(rid[1:]), "class": " ".join(rid) } rowspan = idx_lengths.get((c, r), 0) if rowspan > 1: es["attributes"] = [ format_attr({ "key": "rowspan", "value": rowspan }) ] row_es.append(es) for c, col in enumerate(self.data.columns): cs = [ DATA_CLASS, "row{row}".format(row=r), "col{col}".format(col=c) ] cs.extend(cell_context.get("data", {}).get(r, {}).get(c, [])) formatter = self._display_funcs[(r, c)] value = self.data.iloc[r, c] row_es.append({ "type": "td", "value": value, "class": " ".join(cs), "id": "_".join(cs[1:]), "display_value": formatter(value) }) props = [] for x in ctx[r, c]: # have to handle empty styles like [''] if x.count(":"): props.append(x.split(":")) else: props.append(['', '']) cellstyle.append({ 'props': props, 'selector': "row{row}_col{col}".format(row=r, col=c) }) body.append(row_es) return dict(head=head, cellstyle=cellstyle, body=body, uuid=uuid, precision=precision, table_styles=table_styles, caption=caption, table_attributes=self.table_attributes) def format(self, formatter, subset=None): """ Format the text display value of cells. .. versionadded:: 0.18.0 Parameters ---------- formatter: str, callable, or dict subset: IndexSlice An argument to ``DataFrame.loc`` that restricts which elements ``formatter`` is applied to. Returns ------- self : Styler Notes ----- ``formatter`` is either an ``a`` or a dict ``{column name: a}`` where ``a`` is one of - str: this will be wrapped in: ``a.format(x)`` - callable: called with the value of an individual cell The default display value for numeric values is the "general" (``g``) format with ``pd.options.display.precision`` precision. Examples -------- >>> df = pd.DataFrame(np.random.randn(4, 2), columns=['a', 'b']) >>> df.style.format("{:.2%}") >>> df['c'] = ['a', 'b', 'c', 'd'] >>> df.style.format({'C': str.upper}) """ if subset is None: row_locs = range(len(self.data)) col_locs = range(len(self.data.columns)) else: subset = _non_reducing_slice(subset) if len(subset) == 1: subset = subset, self.data.columns sub_df = self.data.loc[subset] row_locs = self.data.index.get_indexer_for(sub_df.index) col_locs = self.data.columns.get_indexer_for(sub_df.columns) if isinstance(formatter, MutableMapping): for col, col_formatter in formatter.items(): # formatter must be callable, so '{}' are converted to lambdas col_formatter = _maybe_wrap_formatter(col_formatter) col_num = self.data.columns.get_indexer_for([col])[0] for row_num in row_locs: self._display_funcs[(row_num, col_num)] = col_formatter else: # single scalar to format all cells with locs = product(*(row_locs, col_locs)) for i, j in locs: formatter = _maybe_wrap_formatter(formatter) self._display_funcs[(i, j)] = formatter return self def render(self, **kwargs): r""" Render the built up styles to HTML Parameters ---------- **kwargs: Any additional keyword arguments are passed through to ``self.template.render``. This is useful when you need to provide additional variables for a custom template. .. versionadded:: 0.20 Returns ------- rendered: str the rendered HTML Notes ----- ``Styler`` objects have defined the ``_repr_html_`` method which automatically calls ``self.render()`` when it's the last item in a Notebook cell. When calling ``Styler.render()`` directly, wrap the result in ``IPython.display.HTML`` to view the rendered HTML in the notebook. Pandas uses the following keys in render. Arguments passed in ``**kwargs`` take precedence, so think carefuly if you want to override them: * head * cellstyle * body * uuid * precision * table_styles * caption * table_attributes """ self._compute() # TODO: namespace all the pandas keys d = self._translate() # filter out empty styles, every cell will have a class # but the list of props may just be [['', '']]. # so we have the neested anys below trimmed = [ x for x in d['cellstyle'] if any(any(y) for y in x['props']) ] d['cellstyle'] = trimmed d.update(kwargs) return self.template.render(**d) def _update_ctx(self, attrs): """ update the state of the Styler. Collects a mapping of {index_label: ['<property>: <value>']} attrs: Series or DataFrame should contain strings of '<property>: <value>;<prop2>: <val2>' Whitespace shouldn't matter and the final trailing ';' shouldn't matter. """ for row_label, v in attrs.iterrows(): for col_label, col in v.iteritems(): i = self.index.get_indexer([row_label])[0] j = self.columns.get_indexer([col_label])[0] for pair in col.rstrip(";").split(";"): self.ctx[(i, j)].append(pair) def _copy(self, deepcopy=False): styler = Styler(self.data, precision=self.precision, caption=self.caption, uuid=self.uuid, table_styles=self.table_styles) if deepcopy: styler.ctx = copy.deepcopy(self.ctx) styler._todo = copy.deepcopy(self._todo) else: styler.ctx = self.ctx styler._todo = self._todo return styler def __copy__(self): """ Deep copy by default. """ return self._copy(deepcopy=False) def __deepcopy__(self, memo): return self._copy(deepcopy=True) def clear(self): """"Reset" the styler, removing any previously applied styles. Returns None. """ self.ctx.clear() self._todo = [] def _compute(self): """ Execute the style functions built up in `self._todo`. Relies on the conventions that all style functions go through .apply or .applymap. The append styles to apply as tuples of (application method, *args, **kwargs) """ r = self for func, args, kwargs in self._todo: r = func(self)(*args, **kwargs) return r def _apply(self, func, axis=0, subset=None, **kwargs): subset = slice(None) if subset is None else subset subset = _non_reducing_slice(subset) data = self.data.loc[subset] if axis is not None: result = data.apply(func, axis=axis, **kwargs) else: result = func(data, **kwargs) if not isinstance(result, pd.DataFrame): raise TypeError( "Function {func!r} must return a DataFrame when " "passed to `Styler.apply` with axis=None".format( func=func)) if not (result.index.equals(data.index) and result.columns.equals(data.columns)): msg = ('Result of {func!r} must have identical index and ' 'columns as the input'.format(func=func)) raise ValueError(msg) result_shape = result.shape expected_shape = self.data.loc[subset].shape if result_shape != expected_shape: msg = ("Function {func!r} returned the wrong shape.\n" "Result has shape: {res}\n" "Expected shape: {expect}".format(func=func, res=result.shape, expect=expected_shape)) raise ValueError(msg) self._update_ctx(result) return self def apply(self, func, axis=0, subset=None, **kwargs): """ Apply a function column-wise, row-wise, or table-wase, updating the HTML representation with the result. Parameters ---------- func : function ``func`` should take a Series or DataFrame (depending on ``axis``), and return an object with the same shape. Must return a DataFrame with identical index and column labels when ``axis=None`` axis : int, str or None apply to each column (``axis=0`` or ``'index'``) or to each row (``axis=1`` or ``'columns'``) or to the entire DataFrame at once with ``axis=None`` subset : IndexSlice a valid indexer to limit ``data`` to *before* applying the function. Consider using a pandas.IndexSlice kwargs : dict pass along to ``func`` Returns ------- self : Styler Notes ----- The output shape of ``func`` should match the input, i.e. if ``x`` is the input row, column, or table (depending on ``axis``), then ``func(x.shape) == x.shape`` should be true. This is similar to ``DataFrame.apply``, except that ``axis=None`` applies the function to the entire DataFrame at once, rather than column-wise or row-wise. Examples -------- >>> def highlight_max(x): ... return ['background-color: yellow' if v == x.max() else '' for v in x] ... >>> df = pd.DataFrame(np.random.randn(5, 2)) >>> df.style.apply(highlight_max) """ self._todo.append((lambda instance: getattr(instance, '_apply'), (func, axis, subset), kwargs)) return self def _applymap(self, func, subset=None, **kwargs): func = partial(func, **kwargs) # applymap doesn't take kwargs? if subset is None: subset = pd.IndexSlice[:] subset = _non_reducing_slice(subset) result = self.data.loc[subset].applymap(func) self._update_ctx(result) return self def applymap(self, func, subset=None, **kwargs): """ Apply a function elementwise, updating the HTML representation with the result. Parameters ---------- func : function ``func`` should take a scalar and return a scalar subset : IndexSlice a valid indexer to limit ``data`` to *before* applying the function. Consider using a pandas.IndexSlice kwargs : dict pass along to ``func`` Returns ------- self : Styler See Also -------- Styler.where """ self._todo.append((lambda instance: getattr(instance, '_applymap'), (func, subset), kwargs)) return self def where(self, cond, value, other=None, subset=None, **kwargs): """ Apply a function elementwise, updating the HTML representation with a style which is selected in accordance with the return value of a function. .. versionadded:: 0.21.0 Parameters ---------- cond : callable ``cond`` should take a scalar and return a boolean value : str applied when ``cond`` returns true other : str applied when ``cond`` returns false subset : IndexSlice a valid indexer to limit ``data`` to *before* applying the function. Consider using a pandas.IndexSlice kwargs : dict pass along to ``cond`` Returns ------- self : Styler See Also -------- Styler.applymap """ if other is None: other = '' return self.applymap(lambda val: value if cond(val) else other, subset=subset, **kwargs) def set_precision(self, precision): """ Set the precision used to render. Parameters ---------- precision: int Returns ------- self : Styler """ self.precision = precision return self def set_table_attributes(self, attributes): """ Set the table attributes. These are the items that show up in the opening ``<table>`` tag in addition to to automatic (by default) id. Parameters ---------- attributes : string Returns ------- self : Styler Examples -------- >>> df = pd.DataFrame(np.random.randn(10, 4)) >>> df.style.set_table_attributes('class="pure-table"') # ... <table class="pure-table"> ... """ self.table_attributes = attributes return self def export(self): """ Export the styles to applied to the current Styler. Can be applied to a second style with ``Styler.use``. Returns ------- styles: list See Also -------- Styler.use """ return self._todo def use(self, styles): """ Set the styles on the current Styler, possibly using styles from ``Styler.export``. Parameters ---------- styles: list list of style functions Returns ------- self : Styler See Also -------- Styler.export """ self._todo.extend(styles) return self def set_uuid(self, uuid): """ Set the uuid for a Styler. Parameters ---------- uuid: str Returns ------- self : Styler """ self.uuid = uuid return self def set_caption(self, caption): """ Se the caption on a Styler Parameters ---------- caption: str Returns ------- self : Styler """ self.caption = caption return self def set_table_styles(self, table_styles): """ Set the table styles on a Styler. These are placed in a ``<style>`` tag before the generated HTML table. Parameters ---------- table_styles: list Each individual table_style should be a dictionary with ``selector`` and ``props`` keys. ``selector`` should be a CSS selector that the style will be applied to (automatically prefixed by the table's UUID) and ``props`` should be a list of tuples with ``(attribute, value)``. Returns ------- self : Styler Examples -------- >>> df = pd.DataFrame(np.random.randn(10, 4)) >>> df.style.set_table_styles( ... [{'selector': 'tr:hover', ... 'props': [('background-color', 'yellow')]}] ... ) """ self.table_styles = table_styles return self # ----------------------------------------------------------------------- # A collection of "builtin" styles # ----------------------------------------------------------------------- @staticmethod def _highlight_null(v, null_color): return ('background-color: {color}'.format( color=null_color) if pd.isna(v) else '') def highlight_null(self, null_color='red'): """ Shade the background ``null_color`` for missing values. Parameters ---------- null_color: str Returns ------- self : Styler """ self.applymap(self._highlight_null, null_color=null_color) return self def background_gradient(self, cmap='PuBu', low=0, high=0, axis=0, subset=None): """ Color the background in a gradient according to the data in each column (optionally row). Requires matplotlib. Parameters ---------- cmap: str or colormap matplotlib colormap low, high: float compress the range by these values. axis: int or str 1 or 'columns' for columnwise, 0 or 'index' for rowwise subset: IndexSlice a valid slice for ``data`` to limit the style application to Returns ------- self : Styler Notes ----- Tune ``low`` and ``high`` to keep the text legible by not using the entire range of the color map. These extend the range of the data by ``low * (x.max() - x.min())`` and ``high * (x.max() - x.min())`` before normalizing. """ subset = _maybe_numeric_slice(self.data, subset) subset = _non_reducing_slice(subset) self.apply(self._background_gradient, cmap=cmap, subset=subset, axis=axis, low=low, high=high) return self @staticmethod def _background_gradient(s, cmap='PuBu', low=0, high=0): """Color background in a range according to the data.""" with _mpl(Styler.background_gradient) as (plt, colors): rng = s.max() - s.min() # extend lower / upper bounds, compresses color range norm = colors.Normalize(s.min() - (rng * low), s.max() + (rng * high)) # matplotlib modifies inplace? # https://github.com/matplotlib/matplotlib/issues/5427 normed = norm(s.values) c = [colors.rgb2hex(x) for x in plt.cm.get_cmap(cmap)(normed)] return [ 'background-color: {color}'.format(color=color) for color in c ] def set_properties(self, subset=None, **kwargs): """ Convenience method for setting one or more non-data dependent properties or each cell. Parameters ---------- subset: IndexSlice a valid slice for ``data`` to limit the style application to kwargs: dict property: value pairs to be set for each cell Returns ------- self : Styler Examples -------- >>> df = pd.DataFrame(np.random.randn(10, 4)) >>> df.style.set_properties(color="white", align="right") >>> df.style.set_properties(**{'background-color': 'yellow'}) """ values = ';'.join('{p}: {v}'.format(p=p, v=v) for p, v in kwargs.items()) f = lambda x: values return self.applymap(f, subset=subset) @staticmethod def _bar_left(s, color, width, base): """ The minimum value is aligned at the left of the cell Parameters ---------- color: 2-tuple/list, of [``color_negative``, ``color_positive``] width: float A number between 0 or 100. The largest value will cover ``width`` percent of the cell's width base: str The base css format of the cell, e.g.: ``base = 'width: 10em; height: 80%;'`` Returns ------- self : Styler """ normed = width * (s - s.min()) / (s.max() - s.min()) zero_normed = width * (0 - s.min()) / (s.max() - s.min()) attrs = (base + 'background: linear-gradient(90deg,{c} {w:.1f}%, ' 'transparent 0%)') return [ base if x == 0 else attrs.format(c=color[0], w=x) if x < zero_normed else attrs.format(c=color[1], w=x) if x >= zero_normed else base for x in normed ] @staticmethod def _bar_center_zero(s, color, width, base): """ Creates a bar chart where the zero is centered in the cell Parameters ---------- color: 2-tuple/list, of [``color_negative``, ``color_positive``] width: float A number between 0 or 100. The largest value will cover ``width`` percent of the cell's width base: str The base css format of the cell, e.g.: ``base = 'width: 10em; height: 80%;'`` Returns ------- self : Styler """ # Either the min or the max should reach the edge # (50%, centered on zero) m = max(abs(s.min()), abs(s.max())) normed = s * 50 * width / (100.0 * m) attrs_neg = (base + 'background: linear-gradient(90deg, transparent 0%' ', transparent {w:.1f}%, {c} {w:.1f}%, ' '{c} 50%, transparent 50%)') attrs_pos = (base + 'background: linear-gradient(90deg, transparent 0%' ', transparent 50%, {c} 50%, {c} {w:.1f}%, ' 'transparent {w:.1f}%)') return [ attrs_pos.format(c=color[1], w=(50 + x)) if x >= 0 else attrs_neg.format(c=color[0], w=(50 + x)) for x in normed ] @staticmethod def _bar_center_mid(s, color, width, base): """ Creates a bar chart where the midpoint is centered in the cell Parameters ---------- color: 2-tuple/list, of [``color_negative``, ``color_positive``] width: float A number between 0 or 100. The largest value will cover ``width`` percent of the cell's width base: str The base css format of the cell, e.g.: ``base = 'width: 10em; height: 80%;'`` Returns ------- self : Styler """ if s.min() >= 0: # In this case, we place the zero at the left, and the max() should # be at width zero = 0.0 slope = width / s.max() elif s.max() <= 0: # In this case, we place the zero at the right, and the min() # should be at 100-width zero = 100.0 slope = width / -s.min() else: slope = width / (s.max() - s.min()) zero = (100.0 + width) / 2.0 - slope * s.max() normed = zero + slope * s attrs_neg = (base + 'background: linear-gradient(90deg, transparent 0%' ', transparent {w:.1f}%, {c} {w:.1f}%, ' '{c} {zero:.1f}%, transparent {zero:.1f}%)') attrs_pos = (base + 'background: linear-gradient(90deg, transparent 0%' ', transparent {zero:.1f}%, {c} {zero:.1f}%, ' '{c} {w:.1f}%, transparent {w:.1f}%)') return [ attrs_pos.format(c=color[1], zero=zero, w=x) if x > zero else attrs_neg.format(c=color[0], zero=zero, w=x) for x in normed ] def bar(self, subset=None, axis=0, color='#d65f5f', width=100, align='left'): """ Color the background ``color`` proptional to the values in each column. Excludes non-numeric data by default. Parameters ---------- subset: IndexSlice, default None a valid slice for ``data`` to limit the style application to axis: int color: str or 2-tuple/list If a str is passed, the color is the same for both negative and positive numbers. If 2-tuple/list is used, the first element is the color_negative and the second is the color_positive (eg: ['#d65f5f', '#5fba7d']) width: float A number between 0 or 100. The largest value will cover ``width`` percent of the cell's width align : {'left', 'zero',' mid'}, default 'left' - 'left' : the min value starts at the left of the cell - 'zero' : a value of zero is located at the center of the cell - 'mid' : the center of the cell is at (max-min)/2, or if values are all negative (positive) the zero is aligned at the right (left) of the cell .. versionadded:: 0.20.0 Returns ------- self : Styler """ subset = _maybe_numeric_slice(self.data, subset) subset = _non_reducing_slice(subset) base = 'width: 10em; height: 80%;' if not (is_list_like(color)): color = [color, color] elif len(color) == 1: color = [color[0], color[0]] elif len(color) > 2: msg = ("Must pass `color` as string or a list-like" " of length 2: [`color_negative`, `color_positive`]\n" "(eg: color=['#d65f5f', '#5fba7d'])") raise ValueError(msg) if align == 'left': self.apply(self._bar_left, subset=subset, axis=axis, color=color, width=width, base=base) elif align == 'zero': self.apply(self._bar_center_zero, subset=subset, axis=axis, color=color, width=width, base=base) elif align == 'mid': self.apply(self._bar_center_mid, subset=subset, axis=axis, color=color, width=width, base=base) else: msg = ("`align` must be one of {'left', 'zero',' mid'}") raise ValueError(msg) return self def highlight_max(self, subset=None, color='yellow', axis=0): """ Highlight the maximum by shading the background Parameters ---------- subset: IndexSlice, default None a valid slice for ``data`` to limit the style application to color: str, default 'yellow' axis: int, str, or None; default 0 0 or 'index' for columnwise (default), 1 or 'columns' for rowwise, or ``None`` for tablewise Returns ------- self : Styler """ return self._highlight_handler(subset=subset, color=color, axis=axis, max_=True) def highlight_min(self, subset=None, color='yellow', axis=0): """ Highlight the minimum by shading the background Parameters ---------- subset: IndexSlice, default None a valid slice for ``data`` to limit the style application to color: str, default 'yellow' axis: int, str, or None; default 0 0 or 'index' for columnwise (default), 1 or 'columns' for rowwise, or ``None`` for tablewise Returns ------- self : Styler """ return self._highlight_handler(subset=subset, color=color, axis=axis, max_=False) def _highlight_handler(self, subset=None, color='yellow', axis=None, max_=True): subset = _non_reducing_slice(_maybe_numeric_slice(self.data, subset)) self.apply(self._highlight_extrema, color=color, axis=axis, subset=subset, max_=max_) return self @staticmethod def _highlight_extrema(data, color='yellow', max_=True): """Highlight the min or max in a Series or DataFrame""" attr = 'background-color: {0}'.format(color) if data.ndim == 1: # Series from .apply if max_: extrema = data == data.max() else: extrema = data == data.min() return [attr if v else '' for v in extrema] else: # DataFrame from .tee if max_: extrema = data == data.max().max() else: extrema = data == data.min().min() return pd.DataFrame(np.where(extrema, attr, ''), index=data.index, columns=data.columns) @classmethod def from_custom_template(cls, searchpath, name): """ Factory function for creating a subclass of ``Styler`` with a custom template and Jinja environment. Parameters ---------- searchpath : str or list Path or paths of directories containing the templates name : str Name of your custom template to use for rendering Returns ------- MyStyler : subclass of Styler has the correct ``env`` and ``template`` class attributes set. """ loader = ChoiceLoader([ FileSystemLoader(searchpath), cls.loader, ]) class MyStyler(cls): env = Environment(loader=loader) template = env.get_template(name) return MyStyler
def render_j2_template(config, template, data): """ render Jinja2 template :param config: dict of configuration :param template: template (relative path) :param data: dict of data :returns: string of rendered template """ custom_templates = False try: templates_path = config['server']['templates']['path'] env = Environment(loader=FileSystemLoader(templates_path)) custom_templates = True LOGGER.debug('using custom templates: {}'.format(templates_path)) except (KeyError, TypeError): env = Environment(loader=FileSystemLoader(TEMPLATES)) LOGGER.debug('using default templates: {}'.format(TEMPLATES)) env.filters['to_json'] = to_json env.filters['format_datetime'] = format_datetime env.filters['format_duration'] = format_duration env.globals.update(to_json=to_json) env.filters['get_path_basename'] = get_path_basename env.globals.update(get_path_basename=get_path_basename) env.filters['get_breadcrumbs'] = get_breadcrumbs env.globals.update(get_breadcrumbs=get_breadcrumbs) env.filters['filter_dict_by_key_value'] = filter_dict_by_key_value env.globals.update(filter_dict_by_key_value=filter_dict_by_key_value) try: template = env.get_template(template) except TemplateNotFound as err: if custom_templates: LOGGER.debug(err) LOGGER.debug('Custom template not found; using default') env = Environment(loader=FileSystemLoader(TEMPLATES)) template = env.get_template(template) else: raise return template.render(config=config, data=data, version=__version__)
from app.db import db, Profile from app.generate_jwt_token import generate_jwt_token from datetime import datetime from flask import Flask, redirect, make_response, request from jinja2 import Environment, FileSystemLoader from services.team_service import TeamService from os import environ, path from time import mktime accepted_image_mimetypes = ['application/x-yaml', 'text/yaml'] jinja = Environment(loader=FileSystemLoader( path.dirname(path.abspath(__file__))), trim_blocks=True) team_service = TeamService() @app.route('/') def index(): #Go get all the teams in the db teams = team_service.get_teams() return jinja.get_template('list.html.j2').render(teams=teams) @app.route('/<team_id>') def get_team(team_id): #get single team data team = team_service.get_teams(team_id=team_id) return jinja.get_template('team.html.j2').render(team=team)
import subprocess from pathlib import Path from typing import List, Dict, Optional from jinja2 import Environment, FileSystemLoader import yaml from pydantic import BaseModel, Field root = Path(__file__).parent env = Environment(loader=FileSystemLoader(str(root)), ) class Method(BaseModel): params: Dict[str, str] = Field(default_factory=dict) return_: Optional[str] = Field(default="void", alias="return") body: str const: bool = False static: bool = False class Class(BaseModel): dtor: bool = True default_ctor: bool = Field(default=False, alias="default-ctor") copy_ctor: bool = Field(default=False, alias="copy-ctor") trivially: bool = False movable: bool = False eq: bool = False ord: bool = False methods: Dict[str, Method] = Field(default_factory=dict) qobject: bool = False
class MyStyler(cls): env = Environment(loader=loader) template = env.get_template(name)
vault_secrets = var / 'vault-secrets.ini' supervisord_conf = etc / 'supervisord.conf' supervisord_sock = var / 'supervisor' / 'supervisor.sock' @classmethod def load_dashboard(cls, filename): with open(PATH.root / 'grafana-dashboards' / filename) as f: return f.read() @classmethod def get_dashboards(cls): return list(os.listdir(PATH.root / 'grafana-dashboards')) jinja_env = Environment(loader=FileSystemLoader(str(PATH.templates))) jinja_env.globals['int'] = int def render(template_filename, options): template = jinja_env.get_template(template_filename) return template.render(**options) def run(cmd, **kwargs): log.debug('+ %s', cmd) return subprocess.check_output(cmd, shell=True, **kwargs).decode('latin1') config = configparser.ConfigParser() config.read(PATH.cluster_ini)
def main(context, build, clean, tag): """Build your OU Container.""" with open(os.path.join(context, 'ContainerConfig.yaml')) as config_f: settings = load(config_f, Loader=Loader) settings = validate_settings(settings) if isinstance(settings, dict): env = Environment(loader=PackageLoader('ou_container_builder', 'templates'), autoescape=False) if os.path.exists(os.path.join(context, 'ou-builder-build')): shutil.rmtree(os.path.join(context, 'ou-builder-build')) os.makedirs(os.path.join(context, 'ou-builder-build')) # Handle packs if 'packs' in settings and settings['packs']: if 'tutorial-server' in settings['packs']: if 'packages' not in settings: settings['packages'] = {'pip': ['tutorial-server>=0.7.0']} elif 'pip' not in settings['packages']: settings['packages']['pip'] = ['tutorial-server>=0.7.0'] else: settings['packages']['pip'].append( 'tutorial-server>=0.7.0') with open( os.path.join(context, 'ou-builder-build', 'tutorial-server.ini'), 'w') as out_f: tmpl = env.get_template('tutorial-server.ini') out_f.write(tmpl.render(**settings)) # Handle automatic hacks if 'packages' in settings and 'apt' in settings['packages']: if 'openjdk-11-jdk' in settings['packages']['apt']: if 'hacks' in settings: if 'missing-man1' not in settings['hacks']: settings['hacks'].append('missing-man1') else: settings['hacks'] = ['missing-man1'] if settings['type'] == 'jupyter-notebook': jupyter_notebook.generate(context, env, settings) elif settings['type'] == 'web-app': web_app.generate(context, env, settings) if 'content' in settings and settings['content']: with open( os.path.join(context, 'ou-builder-build', 'content_config.yaml'), 'w') as out_f: tmpl = env.get_template('content_config.yaml') out_f.write(tmpl.render(**settings)) if build: cmd = ['docker', 'build', context] if tag: for t in tag: cmd.append('--tag') cmd.append( f'mmh352/{settings["module"]["code"].lower()}' + f'-{settings["module"]["presentation"].lower()}:{t}') subprocess.run(cmd) if clean: os.unlink(os.path.join(context, 'Dockerfile')) if os.path.exists(os.path.join(context, 'ou-builder-build')): shutil.rmtree(os.path.join(context, 'ou-builder-build')) else: click.echo(click.style( 'There are errors in your configuration settings:', fg='red'), err=True) click.echo(err=True) for error in settings: click.echo(error, err=True)
def render(tpl_path, context): path, filename = os.path.split(tpl_path) return jinja2.Environment(loader=jinja2.FileSystemLoader( path or './')).get_template(filename).render(context) path = '/data/dotmaps/' dotmap_cities = [ d for d in os.listdir(path) if os.path.isdir(path + d) and d != 'index_files' ] print dotmap_cities dotmaps = [] for d in dotmap_cities: dotmaps.append({'link': d, 'name': d}) context = {'num': len(dotmap_cities), 'dotmaps': dotmaps} template = open('./dotmap_index_template.html').read() template = template.decode('utf-8') txt = Environment().from_string(template).render(context) f = open('/data/dotmaps/index.html', 'w') f.write(txt.encode('utf-8')) f.close() uid = pwd.getpwnam("www-data").pw_uid gid = grp.getgrnam("www-data").gr_gid os.chown('/data/dotmaps/index.html', uid, gid)
from jinja2 import Environment, PackageLoader, select_autoescape from telegram import InlineKeyboardButton, InlineKeyboardMarkup, TelegramError from telegram.parsemode import ParseMode from transliterate import translit from .models import User from .settings import EMAIL_LOGIN ICON_BOOK_PILE = "📖" ICON_ENVELOPE = "✉" environment = Environment( autoescape=select_autoescape(["markdown"]), loader=PackageLoader("tamizdat", "templates"), lstrip_blocks=True, trim_blocks=True) class NoResponse: def serve(self, bot, message): pass class Response: template_path = NotImplemented def __init__(self): self.template = environment.get_template(self.template_path)
def test_safe(self): env = Environment(autoescape=True) tmpl = env.from_string('{{ "<div>foo</div>"|safe }}') assert tmpl.render() == '<div>foo</div>' tmpl = env.from_string('{{ "<div>foo</div>" }}') assert tmpl.render() == '<div>foo</div>'
import logging from datetime import datetime from functools import wraps from typing import Any, Callable from jinja2 import Environment, FileSystemLoader, select_autoescape from jinja2.environment import Environment, Template from engine.constants import PATHS from engine.utils.misc import none_safe, quote ENV = Environment(loader=FileSystemLoader(PATHS.TEMPL, encoding="utf-8"), autoescape=select_autoescape(enabled_extensions=(), default=False), trim_blocks=True, lstrip_blocks=True, newline_sequence="\n", keep_trailing_newline=True, auto_reload=False) def load_template(path: str, file_type: str = None) -> Callable: """ Load template and pass it as named argument 'template' to function Doesn't handle exceptions. """ def decor(func: Callable) -> Callable: @wraps(func) def wrapper(*args, **kwargs) -> Any: if not file_type:
class PwdHashServer(object): """ A small server for the PwdHash Vault web interface.- """ # # configuration settings for this server # _global_config = { 'server.socket_host': '127.0.0.1', 'server.socket_port': 8080, 'server.thread_pool': 2 } # # site-specific configuration # _site_config = { 'tools.encode.encoding': 'utf-8', 'tools.secureheaders.on': True, 'tools.staticdir.on': True, 'tools.staticdir.dir': '%s/%s' % (current_dir, "static") } def _secure_headers(self): """ These settings provide enhanced security to the served pages.- """ headers = cherrypy.response.headers headers['X-Frame-Options'] = 'DENY' headers['X-XSS-Protection'] = '1; mode=block' headers['Content-Security-Policy'] = "default-src='self'" def __init__(self, pwd_gen): """ Creates a new web application object: pwd_gen the PwdHash generator instance this web app uses.- """ from jinja2 import Environment, PackageLoader self.pwd_gen = pwd_gen # # template-rendering environment # self.jinja_env = Environment( loader=PackageLoader('pwdhash', 'templates')) # # set the security settings on the 'before_finalize' hook point # cherrypy.tools.secureheaders = cherrypy.Tool('before_finalize', self._secure_headers, priority=60) # # turn off logging to standard output # cherrypy.log.screen = None # # initialize the database # self.db = KeyDatabase(current_dir) @cherrypy.expose def about(self): """ The 'about' page.- """ tmpl = self.jinja_env.get_template("about.html") return tmpl.render() @cherrypy.expose def add(self): """ This page allows to save a new key.- """ tmpl = self.jinja_env.get_template("add.html") return tmpl.render() @cherrypy.expose def generate(self, *args, **kwargs): """ This target generates a PwdHash password.- """ from pwdhash.platform import copy_to_clipboard domain = kwargs['domain'] generated = self.pwd_gen.generate(domain) copied_to_clipboard = copy_to_clipboard(generated) if copied_to_clipboard: msg = "Password ready" else: msg = generated del generated return self.index(msg) @cherrypy.expose def pick_image(self, query, start=0): """ Displays images from a Google image search, letting the user select one as the icon for a new entry: query the query string sent to Google images; start the query parameter for pagination.- """ import json import time import urllib import requests BASE_URL = 'https://ajax.googleapis.com/ajax/services/search/images' BASE_URL += '?v=1.0&q=%s' % query BASE_URL += '&start=%d' MAX_RESULTS = 56 img_urls = list() # # display 8 images per page # start = int(start) next_start = start + 8 prev_start = start - 8 # # Google will only return a max of 56 results # if next_start > MAX_RESULTS: next_start = MAX_RESULTS if prev_start < 0: prev_start = None while start < next_start: r = requests.get(BASE_URL % start) for image_info in json.loads(r.text)['responseData']['results']: url = image_info['unescapedUrl'] try: image_r = requests.get(url) img_urls.append(url) except requests.exceptions.ConnectionError: # # ignore images that are not accessible # pass # # we get delivered four images per page # start += 4 # Be nice to Google and they'll be nice back :) time.sleep(0.5) # # disable the link to the next page in case we are at the end # if next_start == MAX_RESULTS: next_start = None # # render the template # tmpl = self.jinja_env.get_template("pick_image.html") return tmpl.render(query=urllib.urlencode({'query': query}), img_urls=img_urls, next_start=next_start, prev_start=prev_start) @cherrypy.expose def index(self, msg=None): """ The 'index' page.- """ # # clean the clipboard if there is no 'msg' # if msg is None: copy_to_clipboard("***") # # get a list of all the available keys # keys = Key.select().orderBy('name') # # render the template # tmpl = self.jinja_env.get_template("index.html") return tmpl.render(keys=keys, msg=msg) @cherrypy.expose def update(self, name=None, domain=None, image=None, delete=None): """ Updates a key in the vault.- """ from sqlobject import SQLObjectNotFound # # delete an entry? # if delete: try: k = Key.byName(delete) Key.delete(k.id) except SQLObjectNotFound: pass # # add or update an entry? # elif name: # # if the entry already exists, we will update it # try: k = Key.byName(name) except SQLObjectNotFound: k = Key(name=name, domain=domain, image=image) # # update the rest of the fields # k.domain = domain k.image = image # # go back home # raise cherrypy.HTTPRedirect("/")
class FlowAppGenerator(object): def __init__(self, app_template_path=None): if not app_template_path: app_template_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'app_template') self.app_template_path = app_template_path super(FlowAppGenerator, self).__init__() def init_env(self, template_path): loader = FileSystemLoader(template_path) self.env = Environment( block_start_string='[%', block_end_string='%]', variable_start_string='[[', variable_end_string=']]', comment_start_string='[#', comment_end_string='#]', loader=loader, ) def gen(self, model_class, detail_model_class=None, wf_code=None, replace=False, ignores=['wfdata.py']): dest = os.path.dirname(inspect.getfile(model_class)) app_name = model_class.__module__.split('.')[-2] if not wf_code: wf_code = app_name ctx = { 'app_name': app_name, 'wf_code': wf_code, 'class_name': model_class.__name__, 'wf_name': model_class._meta.verbose_name, 'field_names': get_field_names(model_class), 'fields': get_fields(model_class), 'grouped_fields': group(get_fields(model_class)), } if detail_model_class: ctx.update({ 'dm__class_name': detail_model_class.__name__, 'dm__field_names': get_field_names(detail_model_class), 'dm__fields': get_fields(detail_model_class), 'dm__grouped__fields': group(get_fields(detail_model_class)), }) self.copy_template(self.app_template_path, dest, ctx, replace, ignores) def copy_template(self, src, dest, ctx={}, replace=False, ignores=[]): self.init_env(src) for path, dirs, files in os.walk(src): relative_path = path[len(src):].lstrip(os.sep) dest_path = os.path.join(dest, relative_path) dest_path = dest_path.replace('app_name', ctx.get('app_name', 'app_name')) if not os.path.exists(dest_path): os.mkdir(dest_path) for i, subdir in enumerate(dirs): if subdir.startswith('.'): del dirs[i] for filename in files: if filename.endswith('.pyc') or filename.startswith('.'): continue src_file_path = os.path.join(path, filename) src_file_path = src_file_path[len(src):].strip('/') dest_file_path = os.path.join(dest, relative_path, filename) dest_file_path = dest_file_path.replace( 'app_name', ctx.get('app_name', 'app_name')) if dest_file_path.endswith('-tpl'): dest_file_path = dest_file_path[:-4] is_exists = os.path.isfile(dest_file_path) for ignore in ignores: if dest_file_path.endswith(ignore): replace = False if is_exists and not replace: continue self.copy_template_file(src_file_path, dest_file_path, ctx) def copy_template_file(self, src, dest, ctx={}): template = self.env.get_template(src) template.stream(ctx).dump(dest, encoding='utf-8') # Make new file writable. if os.access(dest, os.W_OK): st = os.stat(dest) new_permissions = stat.S_IMODE(st.st_mode) | stat.S_IWUSR os.chmod(dest, new_permissions)
os.makedirs(outdir) # create aracne.sh aracne_path = os.path.dirname(os.path.realpath(args.path_to_aracne2.name)) aracne_sh = """#!/bin/bash cd {aracne_path} ./aracne2 $@""" with open(os.path.join(outdir, 'aracne.sh'), 'w') as f: f.write(aracne_sh.format(aracne_path=aracne_path)) os.chmod( f.name, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH) # # create condor script # scriptname = "%s/%s.condor" % (outdir, args.run_id) # use same dir as this file's as environment env = Environment( loader=FileSystemLoader(os.path.dirname(os.path.realpath(__file__)))) template = env.get_template('condor_aracne.tt') with open(scriptname, 'w') as f: f.write( template.render(expfile=expfile, probes=probes, p=p, outdir=outdir, run_id=args.run_id))
def __init__(self): TEMPLATES_ROOT = os.path.abspath(os.path.dirname(__file__)) TEMPLATES_ROOT = os.path.join(TEMPLATES_ROOT, 'templates') self.env = Environment(loader=FileSystemLoader(TEMPLATES_ROOT), trim_blocks=True)
def test_scoping(self): env = Environment(extensions=['jinja2.ext.autoescape']) tmpl = env.from_string( '{% autoescape true %}{% set x = "<x>" %}{{ x }}' '{% endautoescape %}{{ x }}{{ "<y>" }}') assert tmpl.render(x=1) == '<x>1<y>'
#!/usr/bin/env python3 from IPy import IP from yaml import load as yaml_load from jinja2 import Template, Environment, FileSystemLoader environment = Environment(loader=FileSystemLoader('./templates')) with open('network.yml', 'r') as stream: environment.globals = yaml_load(stream) class IPSafeCls(IP): def __init__(self, raw): super().__init__(raw) if self.version() != 4: raise Exception('Only support IPv4') plen = self.prefixlen() if plen % 8 != 0 or plen >= 32: raise Exception('Only suppot /24 /16 /8 /0') self.netips = {} def getnetworkip(self, ipaddr): if isinstance(ipaddr, int): return self[ipaddr] if ipaddr in self.netips: return self.netips[ipaddr] accum = 0 shiftl = 0 for i in reversed(ipaddr.split('.')): accum += int(i, 10) << shiftl shiftl += 8
def test_preprocessor_extension(self): env = Environment(extensions=[PreprocessorExtension]) tmpl = env.from_string('{[[TEST]]}') assert tmpl.render(foo=42) == '{(42)}'
from chalicelib.models_ddb import User, Photo from requests.auth import HTTPBasicAuth from urllib.parse import parse_qs from datetime import datetime, timedelta from jinja2 import Environment, PackageLoader, select_autoescape import requests import uuid import logging import tempfile import io app = Chalice(app_name='cloudalbum') app.debug = True app.log.setLevel(logging.DEBUG) env = Environment(loader=PackageLoader(__name__, 'chalicelib/templates'), autoescape=select_autoescape(['html', 'xml'])) @app.route('/home', methods=['GET'], cors=cors_config) def home(): t = env.get_template('home.html') user = User.get(get_uid(app)) result = Photo.query(user.id) body = t.render(current_user=user, photos=result, presigned_url=util.presigned_url, s3_static_url=S3_STATIC_URL) return Response(body=body, status_code=200,
def test_extension_nodes(self): env = Environment(extensions=[ExampleExtension]) tmpl = env.from_string('{% test %}') assert tmpl.render() == 'False|42|23|{}'
def test_streamfilter_extension(self): env = Environment(extensions=[StreamFilterExtension]) env.globals['gettext'] = lambda x: x.upper() tmpl = env.from_string('Foo _(bar) Baz') out = tmpl.render() assert out == 'Foo BAR Baz'
def test_extend_late(self): env = Environment() env.add_extension('jinja2.ext.autoescape') t = env.from_string( '{% autoescape true %}{{ "<test>" }}{% endautoescape %}') assert t.render() == '<test>'
def test_rebinding(self): original = Environment(extensions=[ExampleExtension]) overlay = original.overlay() for env in original, overlay: for ext in itervalues(env.extensions): assert ext.environment is env