def accordian_collapse(color, title, content, pre, raw_html, **kwargs): """ Creates a collapsible accordian """ title_random = HelperFunctions.id_with_random(5, title) with tag.div(_class="jumbotron container reportng-section-collapsible-class") as h: # with tag.div(id="accordian"): with tag.div(_class="card"): with tag.div(_class="card-header %s reportng-collapse-card-header-class" % color, id="headingOne%s" % title_random): tag.h1(title, _class=color, data_toggle="collapse", data_target="#collapse%s" % title_random, aria_expanded="true", aria_controls="collapse%s" % title_random, id=title_random) with tag.div(id="collapse%s" % title_random, _class="collapse", aria_labelledby="headingOne%s" % title_random, data_parent="#accordion"): with tag.div(_class="card-body context reportng-collapse-card-body-class"): if raw_html != '': raw(raw_html) elif pre: tag.pre(content) else: tag.p(content) if 'alert' in kwargs: HelperFunctions.make_alert(kwargs.get('alert')) if 'badge' in kwargs: HelperFunctions.create_badges(kwargs.get('badge')) return HelperFunctions.convert_to_string(h)
def html(self, root_dir): self.save(root_dir) with p(): with a(href=self.relative_save_path): img_style = "image-rendering:pixelated;" if self.display_height is not None: img_style += 'height:{}px;'.format(self.display_height) img(style=img_style, src=self.relative_save_path) if self.desc is not None: br() pre(self.desc)
def __init__(self, series, **kwargs): super().__init__(f"Normalize the video names in {series.name}") with self.content: if series.normalizable_vids: tags.label("The following video titles can be normalized:") self.addTable(VideoTable(series.normalizable_vids)) example = series.normalizable_vids[0].name tags.pre(f'For example, "{example}" would become ' + f'"{series.normalized_name(example)}"', style="text-align: left;") else: tags.p("Nothing to normalize in this series...") self.submit_tag['disabled'] = True
def report_code_section(self, title, code, **kwargs): """ This section can use used to add code containers that will be lexed and highlighted using highlight.js :param str title: Title of the code section. :param str code: Code. Use pre and code tags so multiline code is fine :param bool section: Kwarg Set to True to append the cards section to the preceding section. Default is false :param tuple alert: Kwarg Create a dismissable alert box. First value of tuple is the color, and the second is the message :param tuple reference: Kwarg Adds a small button which hrefs to a user supplied link. Is a tuple. First value is color, second is link :param dict badge: Kwarg Adds badges. Key is the color, and value is the message. :param dict modal: Create a modal. Is a dictionary. Valid keys are button, title and content :return: a string code section :raises ObjectNotInitiated: Raises exception when the correct flags are not set in ReportWriter Example of how to get code from file: >>> with open('somefile.py', 'r') as f: >>> data = f.read() >>> r += report.report_code_section('my py code', data) """ if not self.code: raise rng.ObjectNotInitiated( 'To integrate code highlighting, set code=True in ReportWriter' ) if 'section' in kwargs: style = rng.CSSControl.sticky_section_css else: style = rng.CSSControl.not_sticky_section with tag.div(_class= "jumbotron container context reportng-code-section-class", style=style) as c: # padding mods t = tag.h1(title, id="%s" % rng.HelperFunctions.id_with_random(5, title)) if 'reference' in kwargs: t.add(rng.HelperFunctions.ref_button(kwargs.get('reference'))) # create dismissable alert box if 'alert' in kwargs: rng.HelperFunctions.make_alert(kwargs.get('alert')) with tag.div( _class="container", style="max-height: 70%; overflow: auto; margin-bottom: 20" ): tag.pre().add(tag.code(code)) if 'badge' in kwargs: rng.HelperFunctions.create_badges(kwargs.get('badge')) if 'modal' in kwargs: if isinstance(kwargs.get('modal'), dict): rng.HelperFunctions.make_modals(title.replace(' ', ''), kwargs.get('modal')) return str(c)
def fence(self, tokens, idx, options, env): token = tokens[idx] # TODO : Later, add correct highlights for languages # info = unescapeAll(token.info).strip() if token.info else "" # langName = "" # if info: # langName = info.split()[0] highlighted = escapeHtml(token.content) if token.attrs: return pre(code(highlighted), **token.attr) else: return pre(code(highlighted))
def _render_header_entries(annotated_entries): for entry, annotated in annotated_entries: with H.pre(**_for_object(entry, 'header-entry')), H.code(): # Dominate defaults to ``__pretty=False`` for ``pre``. _render_known(entry.name) text_node(u': ') _render_annotated(annotated)
def create_page_index(): doc = dominate.document(title="Phone alignments") lexicon = load_lexicon() with doc.head: T.meta(**{'content': 'text/html;charset=utf-8', 'http-equiv': 'Content-Type'}) T.meta(**{'content': 'utf-8', 'http-equiv': 'encoding'}) T.link(rel='stylesheet', href='../style.css') T.script( src="https://code.jquery.com/jquery-3.4.1.min.js", integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=", crossorigin="anonymous", ) T.script(type='text/javascript', src='../script.js') with doc: T.p(""" The web-pages linked below show alignments of phones with lip movement. The data is a subset of 2000 utterances from the Grid corpus. We picked a selection of phones and for each of them we randomly selected at most 128 pairs. """) with T.ul(): for k, g in itertools.groupby(SELECTED_PHONES, key=lambda w: w[0]): with T.li(): for i in g: T.a(i, href=i + "/index.html") T.p("""Mean duration (s) of each phone:""") T.pre(""" AA 0.154 AE 0.066 AH 0.060 AO 0.143 AW 0.277 AY 0.142 EH 0.091 EY 0.124 IH 0.068 IY 0.113 OW 0.123 UW 0.152 """, style="font-family: Courier") path = "www/index.html" with open(path, "w") as f: f.write(doc.render())
def _render_header_entries(annotated_entries): for entry, annotated in annotated_entries: with H.pre(**_for_object(entry, 'header-entry')), H.code(): # Dominate (at least as of 2.2.0) # automatically inlines all descendants of ``pre``. # https://github.com/Knio/dominate/issues/68 _render_known(entry.name) H.span(u': ') _render_annotated(annotated)
def gen_fragments(self, fragments, classes=()): """Serialize a list of `fragments` to HTML.""" with tags.pre(cls=" ".join(("alectryon-io", *classes))) as pre: tags.comment(" Generator: {} ".format(GENERATOR)) fragments = transforms.group_whitespace_with_code(fragments) fragments = transforms.commit_io_annotations(fragments) for fr in fragments: self.gen_fragment(fr) return pre
def make_page(app, uri): doc = dominate.document() inline = False doc.head += tags.script(util.include(root / 'domx' / 'domx.js')) doc.head += tags.style(util.include(root / 'domx' / 'domx.css')) page = app(doc) try: # router for p in uri.path.split('/'): if p == '': continue if p == '_domx': inline = True continue page = getattr(page, p) except AttributeError: raise ValueError(404) # render page tags.pre(repr(page)) with doc: page() # dxify def walk(node): for i in node.children: if not isinstance(i, tags.dom_tag): continue if not isinstance(i, x): walk(i) continue # it's a x() marker # todo onsubmit, etc for different types node['onclick'] = "dx.replace('{}', '{}');".format( i['target'], '/_domx' + i['get'], ) node.children.remove(i) walk(doc.body) if inline: return doc.body.children[0] else: return doc
def _render_message(msg): _render_header_entries(msg.annotated_header_entries) body, transforms = _displayable_body(msg) if body != u'': with H.div(**_for_object(msg.body, u'body-display')): if body is None: H.h3(u'Body is unknown') elif body is Unavailable: H.h3(u'Body is present, but not available for inspection') else: if transforms: H.h3(u'Body after %s' % nicely_join(transforms)) H.pre(body) if msg.trailer_entries: with H.div(_class=u'trailer'): H.h3(u'Headers from the trailer part') _render_header_entries(msg.annotated_trailer_entries)
def _render_message(msg): _render_header_entries(msg.annotated_header_entries) body, transforms = msg.displayable_body if body != u'': with H.div(**_for_object(msg.displayable_body, u'body-display')): if body is None: H.h3(u'Body is unknown') elif isinstance(body, Unavailable): H.h3(u'Body is present, but not available for inspection') else: if transforms: H.h3(u'Body after %s' % nicely_join(transforms)) H.pre(body) if msg.trailer_entries: with H.div(_class=u'trailer'): H.h3(u'Headers from the trailer part') _render_header_entries(msg.annotated_trailer_entries)
def html_report(figs, text=None, name='report', directory=None, dpi=75, fig_dir='figs'): """Produce an HTML report containing figs and text.""" # # if dominate not installed, dive out here if not dominate: return if directory is None: directory = name try: os.mkdir(directory) except FileExistsError as e: print(e) try: os.mkdir(os.path.join(directory, fig_dir)) except FileExistsError as e: print(e) h = html() with h.add(body()).add(div(id='content')): h1(f'Performance Plots: {name}') if text: pre(text) with table().add(tbody()): for j, row in enumerate(figs): r = tr() for i, item in enumerate(row): url = os.path.join(fig_dir, f"{name}-fig-{j}_{i}.png") item.savefig(os.path.join(directory, url), dpi=dpi, bbox_inches='tight') r += td(img(src=url)) with open(os.path.join(directory, name + '.html'), 'w') as f: print(h, file=f) return h
def getCourseTextGraph(): """ Make a pyvis network for our graph. """ coursesAndTexts = g.query(""" select distinct ?courseID ?courseName ?textTitle ?authorLast where { ?courseID a ccso:Course . ?courseID ccso:csName ?courseName . ?courseID ccso:hasLM ?textID . ?textID res:resource ?doc . ?doc dcterms:title ?textTitle . ?doc dcterms:creator ?author . ?author foaf:surname ?authorLast . } """) net = Network(height='750px', width='100%') nxGraph = nx.Graph() for courseID, courseName, textTitle, authorLast in coursesAndTexts: if len(courseName) > 10: courseNameTruncated = courseName[:10] + "..." else: courseNameTruncated = courseName textFormatted = pre('\n'.join( textwrap.wrap(f"{authorLast}, {textTitle}", width=30))).render() nxGraph.add_node(courseID) net.add_node(courseID, shape='dot', mass=3, label=courseNameTruncated, title=courseName, color="#e07678") nxGraph.add_node(authorLast) net.add_node(authorLast, shape='square', mass=4, title=textFormatted) net.add_edge(courseID, authorLast) nxGraph.add_edge(courseID, authorLast) return nxGraph, net
def getHTML(title, info=None, body=None, style=None, state=None, theme=None, icon=None): """Provide HTML object :param str title: short name of the notification, e.g.: server error :param str info: some short description if needed, e.g.: It looks like the server is not responding :param body: it can be string or dominate tag object, e.g.: from dominate import tags as dom return getHTML('server error', body=dom.pre(dom.code(result['Message'])) :param str style: additional css style if needed, e.g.: '.card{color:red;}' :param int state: response state code, if needed, e.g.: 404 :param str theme: message color theme, the same that in bootstrap 5, e.g.: 'warning' :param str icon: awesome icon name, e.g.: 'users' :return: str -- HTML document """ html = document("DIRAC - %s" % title) # select the color to the state code if state in [400, 401, 403, 404]: theme = theme or "warning" elif state in [500]: theme = theme or "danger" elif state in [200]: theme = theme or "success" # select the icon to the theme if theme in ["warning", "warn"]: theme = "warning" icon = icon or "exclamation-triangle" elif theme == "info": icon = icon or "info" elif theme == "success": icon = icon or "check" elif theme in ["error", "danger"]: theme = "danger" icon = icon or "times" else: theme = theme or "secondary" icon = icon or "flask" # If body is text wrap it with tags if body and isinstance(body, six.string_types): body = dom.pre( dom.code(traceback.format_exc() if body == "traceback" else body), cls="mt-5") try: diracLogo = collectMetadata(ignoreErrors=True).get("logoURL", "") except Exception: diracLogo = "" # Create head with html.head: # Meta tags dom.meta(charset="utf-8") dom.meta(name="viewport", content="width=device-width, initial-scale=1") # Favicon dom.link(rel="shortcut icon", href="/static/core/img/icons/system/favicon.ico", type="image/x-icon") # Provide awesome icons # https://fontawesome.com/v4.7/license/ dom.script( src= "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/js/all.min.js" ) # Enable bootstrap 5 # https://getbootstrap.com/docs/5.0/getting-started/introduction/ # https://getbootstrap.com/docs/5.0/about/license/ dom.link( rel="stylesheet", integrity= "sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC", href= "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css", crossorigin="anonymous", ) dom.script( src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js", integrity= "sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM", crossorigin="anonymous", ) # Provide additional css style = ".card{transition:.3s;}.card:hover{transform:scale(1.03);}" + ( style or "") dom.style(style) # Create body with html: # Background image dom.i( cls="position-absolute bottom-0 start-0 translate-middle-x m-5 fa " "fa-%s text-%s" % (icon, theme), style="font-size:40vw;z-index:-1;", ) # A4 page with align center with dom.div( cls= "row vh-100 vw-100 justify-content-md-center align-items-center m-0" ): with dom.div(cls="container", style="max-width:600px;") as page: # Main panel with dom.div(cls="row align-items-center"): # Logo dom.div(dom.img(src=diracLogo, cls="card-img px-2"), cls="col-md-6 my-3") # Information card with dom.div(cls="col-md-6 my-3"): # Show response state number if state and state != 200: dom.div(dom.h1( state, cls="text-center badge bg-%s text-wrap" % theme), cls="row py-2") # Message title with dom.div(cls="row"): dom.div(dom.i(cls="fa fa-%s text-%s" % (icon, theme)), cls="col-auto") dom.div(title, cls="col-auto ps-0 pb-2 fw-bold") # Description if info: dom.small(dom.i(cls="fa fa-info text-info")) dom.small(info, cls="ps-1") # Add content if body: page.add(body) return html.render()
def page_error(): import traceback tags.pre(traceback.format_exc())
def snippet_viz(data, text, clusterer, cluster_by, whitespace_only=False): ''' Visualize appearances of each cluster in the sample text. 'data' should be output like that from get_comp_data''' # Get data for text to use for clustering: tokens = [unidecode.unidecode(char) for char in text] tokens = [ct.tweak_whitespace(w) for w in tokens[0:-1]] print("Starting labelling") labels, _ = hdbscan.approximate_predict(clusterer, data[cluster_by]) clusters = sorted(list(set(labels))) print("Done labelling") # Get snippets for each cluster. snippets = {cluster: [] for cluster in clusters} for i in range(len(data[cluster_by])): corresponding_index = i if "indices" in data: corresponding_index = data["indices"][i] if text[corresponding_index] in [" ", "\n"] or not whitespace_only: # Get a text "snippet" snip = unidecode.unidecode(snippet(text, corresponding_index, k=30)) snippets[labels[i]].append(snip) if args.do_POS: print("Starting POS statistics") tagger = get_tagger() tagged_text = tagger.tag(word_tokenize(text)) POS_stats = {cluster: Counter() for cluster in clusters} POS_stats["total"] = Counter() for i in range(len(data[cluster_by])): corresponding_index = i if "indices" in data: corresponding_index = data["indices"][i] if text[corresponding_index] in [" ", "\n"] or not whitespace_only: prev_word = get_prev_tag(tagged_text, text, corresponding_index) tag = prev_word[1] POS_stats[labels[i]][tag] += 1 POS_stats["total"][tag] += 1 if i % 1000 == 0: print("Done up to index %d" % corresponding_index) print("Done POS Statistics") content = dt.div(dt.h1("Cluster Summary")) for k, v in snippets.items(): print("DEBUG: - Visualizing cluster %s" % k) details = dt.details(dt.summary("Cluster: %s" % k)) random.shuffle(v) # Randomize list of snippets if len(v) > 300: v = v[:300] # Shorten list of snippets to manageable size for snip in v: details.add(dt.div(snip)) if args.do_POS: # Summarize statistics summary = "" total_instances = sum(POS_stats[k].values()) for tag, count in POS_stats[k].items(): total_tag_count = POS_stats["total"][tag] precision = (count * 1.0) / total_instances recall = (count * 1.0) / total_tag_count percentage = 1.0 * count / total_tag_count summary += "%s: %d (Precision: %.4f, Recall: %.4f)\n" % ( tag, count, precision, recall) details.add(dt.pre(summary)) content.add(details) return content
def report_section(self, title, content, html_content='', pre_tag=True, tag_color='default', title_color=True, overflow=rng.CSSControl.css_overflow, text_color='primary', h2_title=False, **kwargs): """ This form the main body of the report :param str title: The h1/header title of the section :param bool pre_tag: Default is True and treats content as monospaced. Set to False to use p tag :param str content: The content for this section :param str tag_color: The severity color of the section. :param str text_color: Controls the color of the text. Use red, blue, green, yellow etc :param str overflow: Allows to control the style of the div container. Defaults to scroll on overflow. Set to empty string to have all content show :param bool h2_title: Set to True if the title should be converted to a h2 tag. Will not apprear in section and cannot modify colors :param bool title_color: Controls if the header background or text is colored. Default is True and lets background color. :param tuple alert: Kwarg Create a dismissable alert box. First value of tuple is the color, and the second is the message :param bool section: Kwarg Set to True to append the section to the preceding section. Default is false :param tuple reference: Kwarg Adds a small button which hrefs to a user supplied link. Is a tuple. First value is color, second is link :param dict badge: Kwarg Adds badges. Key is the color, and value is the message. :param str html_content: Insert raw html to be added to the end of the section :param dict modal: Create a modal. Is a dictionary. Valid keys are button, title and content :return: a jumbotron object :raises NotValidTag: Raises exception if a valid tag is not used Example show how to use a red title with only colored text >>> r += report.report_section('some title', content, tag_color='warning', titble_bg=False) """ if title_color: color = 'bg' else: color = 'text' if tag_color not in rng.HelperFunctions.valid_tags: raise rng.NotValidTag( 'Valid tags are primary secondary success danger warning info') # create a space between body jumbotrons tag.br() if 'section' in kwargs: style = rng.CSSControl.sticky_section_css else: style = rng.CSSControl.not_sticky_section # creates the jumbotron. User dictates if it is pre or p tag with tag.div( _class= "jumbotron container context reportng-report-section-class", style=style) as r: # padding mods # can change the text color, or the background color if h2_title: tag.h2(title) else: t = tag.h1( title, _class="%s-%s" % (color, rng.HelperFunctions.color_to_tag(tag_color)), id="%s" % rng.HelperFunctions.id_with_random(5, title)) if 'reference' in kwargs: t.add(rng.HelperFunctions.ref_button(kwargs.get('reference'))) # creates dismissable alert box if 'alert' in kwargs: rng.HelperFunctions.make_alert(kwargs.get('alert')) # creates a reference button with link with tag.div(_class="container", style=overflow): if pre_tag: tag.pre(content, _class="text-%s" % rng.HelperFunctions.color_to_tag(text_color)) else: tag.p(content, _class="text-%s" % rng.HelperFunctions.color_to_tag(text_color)) if 'badge' in kwargs: rng.HelperFunctions.create_badges(kwargs.get('badge')) if html_content != '': raw(html_content) if 'modal' in kwargs: if isinstance(kwargs.get('modal'), dict): rng.HelperFunctions.make_modals(title.replace(' ', ''), kwargs.get('modal')) return str(rng.HelperFunctions.convert_to_string(r))