def list_views(self, request): available = [] mf = MakeFiguresNDP(None) for x in sorted(mf.available()): data_formats = mf.available_formats(x) available.append((x, data_formats)) return { 'available': available, 'navigation': self.get_navigation_links(request), }
def go(): l = self.get_library(request) id_ndp = self.get_model_name(request) context = l._generate_context_with_hooks() ndp = l.load_ndp(id_ndp, context) mf = MakeFiguresNDP(ndp=ndp, library=l, yourname=id_ndp) data_format = str(request.matchdict['format']) data = mf.get_figure('ndp_graph_templatized', data_format) mime = get_mime_for_format(data_format) return response_data(request=request, data=data, content_type=mime)
def __call__(self, data): ndp = get_ndp(data) library = data['library'] mf = MakeFiguresNDP(ndp=ndp, library=library, yourname=None) formats = mf.available_formats(self.name) res = mf.get_figure(self.name, formats) results = [(_, self.name, res[_]) for _ in formats] return results
def callback(tag0): source_code = tag0.string.encode('utf-8') context = Context() ndp = library.parse_ndp(source_code, realpath=realpath, context=context) mf = MakeFiguresNDP(ndp=ndp, library=library, yourname=None) # XXX formats = ['svg'] if generate_pdf: formats.append('pdf') data = mf.get_figure(which,formats) tag = make_tag(tag0, which, data, ndp=ndp, template=None) return tag
def allformats(context, id_ndp, ndp, libname): mf = MakeFiguresNDP(ndp=ndp, library=None, yourname=None) for name in mf.available(): do_it = libname in ['basic', 'libtemplates', 'solver'] or \ toss_coin(id_ndp, MCDPConstants.test_fraction_of_allreports) if do_it: r = context.comp(allformats_report, id_ndp, ndp, libname, name, job_id=name) context.add_report(r, 'allformats', id_ndp=id_ndp, which=name)
def figint01(): ndp = parse_ndp(""" mcdp { } """) mf = MakeFiguresNDP(ndp=ndp, library=None, yourname=None) for name in mf.available(): formats = mf.available_formats(name) res = mf.get_figure(name, formats) print('%s -> %s %s ' % (name, formats, map(len, [res[f] for f in formats])))
def allformats_report(id_ndp, ndp, libname, which): from mcdp_web.images.images import get_mime_for_format r = Report(id_ndp + '-' + which) from mcdp_library_tests.tests import get_test_library library = get_test_library(libname) mf = MakeFiguresNDP(ndp=ndp, library=library, yourname=id_ndp) formats = mf.available_formats(which) try: res = mf.get_figure(which, formats) except DPSemanticError as e: if 'Cannot abstract' in str(e): r.text('warning', 'Not connected. \n\n %s' % e) return r print('%s -> %s %s ' % (which, formats, map(len, [res[f] for f in formats]))) fig = r.figure() for f in formats: data = res[f] mime = get_mime_for_format(f) dn = DataNode(f, data=data, mime=mime) fig.add_child(dn) return r
def config(self, config): config.add_route('solver_image', '/libraries/{library}/models/{model_name}/views/solver/compact_graph.{format}') config.add_view(self.view_ndp_graph_templatized, route_name='solver_image') config.add_route('solver_image2', '/libraries/{library}/models/{model_name}/views/solver/{fun_axes}/{res_axes}/compact_graph.{format}') config.add_view(self.view_ndp_graph_templatized, route_name='solver_image2') config.add_route('solver2_image', '/libraries/{library}/models/{model_name}/views/solver2/compact_graph.{format}') config.add_view(self.view_ndp_graph_templatized, route_name='solver2_image') mf = MakeFiguresNDP(None) for x in mf.available(): route_name = 'make_figures_%s' % x url = self.get_lmv_url('{library}', '{model_name}', 'images') + '{which}.{format}' config.add_route(route_name, url) config.add_view(self.make_figures, route_name=route_name) route_name = 'list_views' url = self.get_lmv_url('{library}', '{model_name}', 'images') config.add_route(route_name, url) config.add_view(self.list_views, route_name=route_name, renderer='images/list_views.jinja2')
class MFCall(): def __init__(self, name): self.name = name def __call__(self, data): ndp = get_ndp(data) library = data['library'] mf = MakeFiguresNDP(ndp=ndp, library=library, yourname=None) formats = mf.available_formats(self.name) res = mf.get_figure(self.name, formats) results = [(_, self.name, res[_]) for _ in formats] return results mf = MakeFiguresNDP(ndp=None) for name in mf.available(): allplots.add((name, MFCall(name))) # # class DP_MFCall(): # def __init__(self, name): # self.name = name # # def __call__(self, data): # dp = get_dp(data) # # mf = MakeFiguresDP(dp=dp) # formats = mf.available_formats(self.name) # res = mf.get_figure(self.name, formats) # results = [(_, self.name, res[_]) for _ in formats]
def get_png_data_model(library, name, ndp, data_format): mf = MakeFiguresNDP(ndp=ndp, library=library, yourname=name) f = 'fancy_editor' res = mf.get_figure(f, data_format) return res
def make_figures(library, frag, raise_error_dp, raise_error_others, realpath, generate_pdf): """ Looks for codes like: <pre><code class="mcdp_ndp_graph_templatized">mcdp { # empty model } </code></pre> and creates a link to the image """ soup = bs(frag) def go(selector, func): for tag in soup.select(selector): try: r = func(tag) tag.replaceWith(r) except (DPSyntaxError, DPSemanticError) as e: if raise_error_dp: raise print(e) t = soup.new_tag('pre', **{'class': 'error %s' % type(e).__name__}) t.string = str(e) tag.insert_after(t) except Exception as e: if raise_error_others: raise t = soup.new_tag('pre', **{'class': 'error %s' % type(e).__name__}) t.string = traceback.format_exc(e) tag.insert_after(t) def make_tag(tag0, klass, data, ndp=None, template=None): if False: png = data['png'] r = create_img_png_base64(soup, png, **{'class': klass}) return r else: svg = data['svg'] tag_svg = BeautifulSoup(svg, 'lxml', from_encoding='utf-8').svg tag_svg['class'] = klass if tag0.has_attr('style'): tag_svg['style'] = tag0['style'] if tag0.has_attr('id'): tag_svg['id'] = tag0['id'] if generate_pdf: pdf0 = data['pdf'] pdf = crop_pdf(pdf0, margins=0) div = soup.new_tag('div') if tag0.has_attr('id'): basename = tag0['id'] elif ndp is not None and hasattr(ndp, ATTR_LOAD_NAME): basename = getattr(ndp, ATTR_LOAD_NAME) elif template is not None and hasattr(template, ATTR_LOAD_NAME): basename = getattr(template, ATTR_LOAD_NAME) else: hashcode = hashlib.sha224(tag0.string).hexdigest()[-8:] basename = 'code-%s' % (hashcode) docname = os.path.splitext(os.path.basename(realpath))[0] download = docname + "." + basename + "." + klass + '.pdf' a = create_a_to_data(soup, download=download, data_format='pdf', data=pdf) a['class'] = 'pdf_data' a.append(NavigableString(download)) div.append(tag_svg) div.append(a) return div else: return tag_svg mf = MakeFiguresNDP(None,None,None) available = set(mf.available()) | set(mf.aliases) for which in available: selector = 'pre.%s' % which def callback(tag0): source_code = tag0.string.encode('utf-8') context = Context() ndp = library.parse_ndp(source_code, realpath=realpath, context=context) mf = MakeFiguresNDP(ndp=ndp, library=library, yourname=None) # XXX formats = ['svg'] if generate_pdf: formats.append('pdf') data = mf.get_figure(which,formats) tag = make_tag(tag0, which, data, ndp=ndp, template=None) return tag go(selector, callback) mf = MakeFiguresTemplate(None,None,None) available = set(mf.available()) | set(mf.aliases) for which in available: selector = 'pre.%s' % which def callback(tag0): source_code = tag0.string.encode('utf-8') context = Context() template = library.parse_template(source_code, realpath=realpath, context=context) mf = MakeFiguresTemplate(template=template, library=library, yourname=None) # XXX formats = ['svg'] if generate_pdf: formats.append('pdf') data = mf.get_figure(which,formats) tag = make_tag(tag0, which, data, ndp=None, template=template) return tag go(selector, callback) # go('pre.ndp_graph_normal', ndp_graph_normal_) # go('pre.ndp_graph_templatized', ndp_graph_templatized_) # go('pre.ndp_graph_templatized_labeled', ndp_graph_templatized_labeled_) # go('pre.ndp_graph_enclosed', ndp_graph_enclosed_) # go('pre.ndp_graph_expand', ndp_graph_expand_) # go('pre.template_graph_enclosed', template_graph_enclosed_) return str(soup)