def generate(codes, output_path = './qrmaster', url='http://test.com', title='', img_path=None): """codes are tuples in the form: (code, id)""" module_path, _ = path.split(__file__) css_file_name = 'style.css' template_path = path.join(module_path, 'template.html') css_path = path.join(module_path, css_file_name) output_file = path.join(output_path, 'qrmaster.html') o_css_path = path.join(output_path, css_file_name) files_path = path.join(output_path, 'qrmaster') if not path.exists(files_path): makedirs(files_path) if img_path: _, img_file_name = path.split(img_path) o_img_path = path.join(output_path, img_file_name) copyfile(img_path, o_img_path) else: o_img_path = '' copyfile(css_path, o_css_path) data = [] for c in codes: qr = qrcode.QRCode( version = 1, box_size = 5, border = 0, error_correction = qrcode.constants.ERROR_CORRECT_H ) full_url = urljoin(url, c[0]) c.append(full_url) data.append(c) qr.add_data(full_url) qr.make() qr_path = path.join(files_path, c[0]+'.svg') qr.make_image( image_factory=qrcode.image.svg.SvgImage).save( qr_path) with open(template_path, 'r') as f: tmp = f.read() html = Template(tmp).generate(data=data, files_path=files_path, title=title, img_path=img_file_name) with open(output_file, 'w') as f: f.write( html.decode('utf-8') )
def generate(codes, output_path='./qrmaster', url='http://test.com', title='', img_path=None): """codes are tuples in the form: (code, id)""" module_path, _ = path.split(__file__) css_file_name = 'style.css' template_path = path.join(module_path, 'template.html') css_path = path.join(module_path, css_file_name) output_file = path.join(output_path, 'qrmaster.html') o_css_path = path.join(output_path, css_file_name) files_path = path.join(output_path, 'qrmaster') if not path.exists(files_path): makedirs(files_path) if img_path: _, img_file_name = path.split(img_path) o_img_path = path.join(output_path, img_file_name) copyfile(img_path, o_img_path) else: o_img_path = '' copyfile(css_path, o_css_path) data = [] for c in codes: qr = qrcode.QRCode(version=1, box_size=5, border=0, error_correction=qrcode.constants.ERROR_CORRECT_H) full_url = urljoin(url, c[0]) c.append(full_url) data.append(c) qr.add_data(full_url) qr.make() qr_path = path.join(files_path, c[0] + '.svg') qr.make_image(image_factory=qrcode.image.svg.SvgImage).save(qr_path) with open(template_path, 'r') as f: tmp = f.read() html = Template(tmp).generate(data=data, files_path=files_path, title=title, img_path=img_file_name) with open(output_file, 'w') as f: f.write(html.decode('utf-8'))
def render_live_template(handler): """Given a narrative ID and df records, render the template.""" orgdf = get_original_df(handler) nrid = handler.args['nrid'][0] if not nrid.endswith('.json'): nrid += '.json' data = json.loads(handler.args['data'][0]) df = pd.DataFrame.from_records(data) nrpath = op.join(nlg_path, handler.current_user.email, nrid) with open(nrpath, 'r') as fout: # noqa: No encoding for json templates = json.load(fout) narratives = [] style = json.loads(handler.args['style'][0]) for t in templates['config']: tmpl = utils.add_html_styling(t['template'], style) s = Template(tmpl).generate(df=df, fh_args=t.get('fh_args', {}), G=grammar, U=utils, orgdf=orgdf) rendered = s.decode('utf8') narratives.append(rendered) return '\n'.join(narratives)