def donate(): try: form = DonatorForm(request.form) if form.validate(): # temporary naughty way try: donator_obj = Donator.objects.get( email=form.email.data.lower()) except (ValidationError, DoesNotExist): donator_obj = Donator(email=form.email.data.lower(), nickname=form.nickname.data) donator_obj.save() donate_obj = Donate(amount=form.amount.data, donator=donator_obj) donate_obj.save() cl = Client(current_app.config['ZARINPAL_WEBSERVICE']) result = cl.service.PaymentRequest( current_app.config['MMERCHANT_ID'], donate_obj.amount, u'هدیه از طرف %s' % donator_obj.name, donator_obj.email, '', str( url_for('main.donate_callback', _external=True, donate_id=donate_obj.pk))) if result.Status == 100: # connect to bank here return jsonify({ 'status': 1, 'redirect': 'https://www.zarinpal.com/pg/StartPay/' + result.Authority }) else: return jsonify({ 'status': 3, 'error': english_num_to_persian(result.Status), 'form': minify(render_template('donate_form.html', form=form)) }) return jsonify({ 'status': 2, 'form': minify(render_template('donate_form.html', form=form)) }) except Exception as e: traceback.print_exc() print e.message return abort(500)
def to_file(self, output_file: Path or str, silent: bool = True) -> None: """Write the report to a file. By default a name is generated. Args: output_file: The name or the path of the file to generate including the extension (.html). silent: if False, opens the file in the default browser """ if type(output_file) == str: output_file = Path(output_file) with output_file.open("w", encoding="utf8") as f: wrapped_html = self.to_html() if self.minify_html: from htmlmin.main import minify wrapped_html = minify(wrapped_html, remove_all_empty_space=True, remove_comments=True) f.write(wrapped_html) if not silent: import webbrowser webbrowser.open_new_tab(output_file)
def _render_html(self) -> str: from pandas_profiling.report.presentation.flavours import HTMLReport report = self.report with tqdm(total=1, desc="Render HTML", disable=not self.config.progress_bar) as pbar: html = HTMLReport(copy.deepcopy(report)).render( nav=self.config.html.navbar_show, offline=self.config.html.use_local_assets, inline=self.config.html.inline, assets_prefix=self.config.html.assets_prefix, primary_color=self.config.html.style.primary_color, logo=self.config.html.style.logo, theme=self.config.html.style.theme, title=self.description_set["analysis"]["title"], date=self.description_set["analysis"]["date_start"], version=self.description_set["package"] ["pandas_profiling_version"], ) if self.config.html.minify_html: from htmlmin.main import minify html = minify(html, remove_all_empty_space=True, remove_comments=True) pbar.update() return html
def response_minify(response): if response.content_type == u'text/html; charset=utf-8': response.set_data( minify(response.get_data(as_text=True)) ) return response return response
def _render_html(self): from pandas_profiling.report.presentation.flavours import HTMLReport report = self.report disable_progress_bar = not config["progress_bar"].get(bool) with tqdm(total=1, desc="Render HTML", disable=disable_progress_bar) as pbar: html = HTMLReport(report).render( nav=config["html"]["navbar_show"].get(bool), offline=config["html"]["use_local_assets"].get(bool), inline=config["html"]["inline"].get(bool), file_name=Path(config["html"]["file_name"].get(str)).stem, primary_color=config["html"]["style"]["primary_color"].get( str), logo=config["html"]["style"]["logo"].get(str), theme=config["html"]["style"]["theme"].get(str), title=self.description_set["analysis"]["title"], date=self.description_set["analysis"]["date_start"], version=self.description_set["package"] ["pandas_profiling_version"], ) minify_html = config["html"]["minify_html"].get(bool) if minify_html: from htmlmin.main import minify html = minify(html, remove_all_empty_space=True, remove_comments=True) pbar.update() return html
def _jinja2_filter_minify(content): """ Strip the line breaks in content :param content: :return: """ return minify(content)
def minify_html_response(response): """Minify any responses that contain HTML. """ if response.content_type == u"text/html; charset=utf-8" and \ current_app.config["MINIFY_HTML"]: response.set_data(minify(response.get_data(as_text=True))) return response return response
def response_minify(response): """ Minify html response to decrease site traffic """ if response.content_type == u'text/html; charset=utf-8': data = minify(response.get_data(as_text=True)) response.set_data(data) return response return response
def minifier(content): if not IS_PYTHON_3 and isinstance(content, str): content = unicode(content, 'utf-8') return minify(content, remove_comments=True, reduce_empty_attributes=True, remove_optional_attribute_quotes=False)
def response_minify(response): """ minify html response to decrease site traffic """ if response.content_type == "text/html; charset=utf-8": response.set_data(minify(response.get_data(as_text=True))) return response return response
def response_minify(response): """ minify html response to decrease site traffic """ from htmlmin.main import minify if response.content_type == u'text/html; charset=utf-8': response.set_data(minify(response.get_data(as_text=True))) return response return response
def minifier(content): if not IS_PYTHON_3 and isinstance(content, str): content = unicode(content, 'utf-8') resp = minify(content, remove_comments=True, reduce_empty_attributes=True, remove_optional_attribute_quotes=False) logger.debug("Response minifier: {}".format(resp.get_data())) return resp
def render_minify_template(template_name_or_list, **context): """ Use this method will minify the templates when 'ENV' is set 'to production' or the 'enable_minify' parameter is set to True :param template_name_or_list: :param context: :return: """ if app.config['ENABLE_MINIFY'] or app.config['ENV'] == 'production': return minify(render_template(template_name_or_list, **context)) else: return render_template(template_name_or_list, **context)
def response_minify(response): if debug: return response """ minify html response to decrease site traffic """ if response.content_type == u'text/html; charset=utf-8': response.set_data( minify(response.get_data(as_text=True), remove_comments=True, remove_empty_space=True, remove_all_empty_space=True, reduce_empty_attributes=True, reduce_boolean_attributes=False, remove_optional_attribute_quotes=True, convert_charrefs=False) ) return response return response
def response_minify(self, response): """ minify response html to decrease traffic """ if response.content_type == u'text/html; charset=utf-8': response.set_data( minify(response.get_data(as_text=True), remove_comments=True) ) return response return response
def minify_response(response): """Minify response to save bandwith.""" if response.mimetype == u'text/html': data = response.get_data(as_text=True) response.set_data( minify(data, remove_comments=True, remove_empty_space=True, reduce_boolean_attributes=True)) return response
def minify_response(response): """minify html response to decrease site traffic""" if response.content_type == u'text/html; charset=utf-8': response.set_data( minify( response.get_data(as_text=True), remove_comments=True, remove_empty_space=True ) ) return response return response
def response_minify(response): """ minify response html to decrease traffic """ if response.content_type == u'text/html; charset=utf-8': response.direct_passthrough = False response.set_data( minify(response.get_data(as_text=True), remove_comments=True, reduce_empty_attributes=True, remove_optional_attribute_quotes=False) ) return response return response
def response_minify(response): """ minify response html to decrease traffic """ if response.content_type == 'text/html; charset=utf-8': response.direct_passthrough = False response.set_data( minify(response.get_data(as_text=True), remove_comments=True, reduce_empty_attributes=True, remove_optional_attribute_quotes=False)) return response return response
def response_minify(self, response): """ minify response html to decrease traffic """ if response.content_type == u'text/html; charset=utf-8': response_text = response.get_data(as_text=True) htmlmin_kwargs = dict(reduce_empty_attributes=False, remove_comments=True, remove_optional_attribute_quotes=False) minified = minify(response_text, **htmlmin_kwargs) response.set_data(minified) return response return response
def response_minify(response): # pylint: disable=unused-variable """ minify html response to decrease site traffic """ if (application.config["EQ_ENABLE_HTML_MINIFY"] and response.content_type == "text/html; charset=utf-8"): response.set_data( minify( response.get_data(as_text=True), remove_comments=True, remove_empty_space=True, remove_optional_attribute_quotes=False, )) return response return response
def after_request(response): ''' save rendered page ''' from htmlmin.main import minify import os if response.content_type == u'text/html; charset=utf-8': # minify response.set_data(minify(response.get_data(as_text=True))) # update static dir try: os.makedirs('%s/_site%s' % (os.getcwd(), request.path)) except OSError: pass rendered = open('%s/_site%s/index.html' % \ (os.getcwd(), request.path), 'w') rendered.write(response.data) return response
def response_handler(response): """Minify HTML and use gzip compression.""" if response.mimetype == "text/html": response.set_data(minify(response.get_data(as_text=True))) if response.content_length < 500: # do not gzip below 500 bytes return response response.direct_passthrough = False gzip_buffer = BytesIO() gzip_file = gzip.GzipFile(mode="wb", compresslevel=6, fileobj=gzip_buffer) gzip_file.write(response.get_data()) gzip_file.close() response.set_data(gzip_buffer.getvalue()) response.headers.add("Content-Encoding", "gzip") return response
def to_html(self) -> str: """Generate and return complete template as lengthy string for using with frameworks. Returns: Profiling report html including wrapper. """ if len(self.report.content["items"]) == 0: warnings.warn(""" sections is empty""") from .report.presentation.flavours import HTMLReport from .report.presentation.flavours.html import templates use_local_assets = config["html"]["use_local_assets"].get(bool) html = HTMLReport(self.report).render() nav_items = [(section.name, section.anchor_id) for section in self.report.content["items"]] # TODO: move to structure wrapped_html = templates.template("wrapper/wrapper.html").render( content=html, title=self.title, nav=config["html"]["navbar_show"].get(bool), nav_items=nav_items, version=__version__, offline=use_local_assets, primary_color=config["html"]["style"]["primary_color"].get(str), logo=config["html"]["style"]["logo"].get(str), theme=config["html"]["style"]["theme"].get(str), ) minify_html = config["html"]["minify_html"].get(bool) if minify_html: from htmlmin.main import minify wrapped_html = minify(wrapped_html, remove_all_empty_space=True, remove_comments=True) return wrapped_html
def donate(): try: form = DonatorForm(request.form) if form.validate(): # temporary naughty way try: donator_obj = Donator.objects.get(email=form.email.data.lower()) except (ValidationError, DoesNotExist): donator_obj = Donator(email=form.email.data.lower(), nickname=form.nickname.data) donator_obj.save() donate_obj = Donate(amount=form.amount.data, donator=donator_obj) donate_obj.save() cl = Client(current_app.config['ZARINPAL_WEBSERVICE']) result = cl.service.PaymentRequest(current_app.config['MMERCHANT_ID'], donate_obj.amount, u'هدیه از طرف %s' % donator_obj.name, donator_obj.email, '', str(url_for('main.donate_callback', _external=True, donate_id=donate_obj.pk))) if result.Status == 100: # connect to bank here return jsonify({'status': 1, 'redirect': 'https://www.zarinpal.com/pg/StartPay/' + result.Authority}) else: return jsonify({'status': 3, 'error': english_num_to_persian(result.Status), 'form': minify(render_template('donate_form.html', form=form))}) return jsonify({'status': 2, 'form': minify(render_template('donate_form.html', form=form))}) except Exception as e: traceback.print_exc() print e.message return abort(500)
def to_html(self) -> str: """Generate and return complete template as lengthy string for using with frameworks. Returns: Profiling report html including wrapper. """ from pandas_profiling.report.presentation.flavours import HTMLReport from pandas_profiling.report.presentation.flavours.html import templates use_local_assets = config["html"]["use_local_assets"].get(bool) html = HTMLReport(self.report).render() # TODO: move to structure wrapped_html = templates.template("wrapper/wrapper.html").render( content=html, title=self.title, correlation=len(self.description_set["correlations"]) > 0, missing=len(self.description_set["missing"]) > 0, scatter=len(self.description_set["scatter"]) > 0, sample=len(self.sample) > 0, version=__version__, offline=use_local_assets, primary_color=config["html"]["style"]["primary_color"].get(str), logo=config["html"]["style"]["logo"].get(str), theme=config["html"]["style"]["theme"].get(str), ) minify_html = config["html"]["minify_html"].get(bool) if minify_html: from htmlmin.main import minify wrapped_html = minify(wrapped_html, remove_all_empty_space=True, remove_comments=True) return wrapped_html
def wrapped(*args, **kwargs): yielded_html = route_function(*args, **kwargs) minified_html = minify(yielded_html) return minified_html
def response_minify(response): """Minify HTML response to reduce bandwidth""" if response.content_type == u'text/html; charset=utf-8': response.set_data(minify(response.get_data(as_text=True))) return response
def html_minify(response): """Minify html responses.""" if response.content_type == "text/html; charset=utf-8": response.set_data(minify(response.get_data(as_text=True))) return response
#!/usr/bin/env python from htmlmin import main as htmlmin import codecs import os base_path = 'arduino/WifiWebServer/' html_folder = 'html' pages = os.listdir(os.path.join(base_path, html_folder)) for page in pages: path = os.path.join(base_path, html_folder, page) if page.endswith('.html') and os.path.isfile(path): with codecs.open(path, encoding='utf-8') as f: html = f.read() ohtml = htmlmin.minify(html).replace('\n', '\\\n').replace('"', '\\"') page_name = os.path.splitext(page)[0] header_path = os.path.join(base_path, '%s.h' % page_name) with codecs.open(header_path, encoding='utf-8', mode='w') as of: of.write('static const char %s[] = "%s";' % (page_name.upper(), ohtml)) print 'Create %s' % header_path
def response_minify(response): """ minify and muncher html """ if response.content_type == u'text/html; charset=utf-8': template = None if str(request.path)=='/view1' or str(request.path)=='/view2' : #Extractor of CSS Links soup = BeautifulSoup(response.get_data(as_text=True),features='html.parser') cssGroupLinks = '' #Find the lines with the word 'link' for link in soup.find_all('link'): #If you find the substring 'CSS' in it then .. if 'css' in link.get('href'): #Get the link link_css = link.get('href') #Get file fullpath file_fpath = str(os.getcwd())+link_css.replace('/','\\') #Generate a custom concatenate for Muncher if cssGroupLinks == '': cssGroupLinks = file_fpath else: cssGroupLinks = cssGroupLinks + ',' + file_fpath #Name of the file encrypted with the unix time and the ip address of the browser file_tmp = encrypt_string(str(time.time()) + request.remote_addr) #Html Compressor compress_site = minify(response.get_data(as_text=True)) #Search and replace the css original with the new compiled. compress_site = compress_site.replace('.css', '.opt.css') #Deposit tmp file for Muncher temp_html_file = open("tmp/"+file_tmp+".html","w") temp_html_file.write(compress_site) temp_html_file.close() #Get files fullpath template = str(os.getcwd())+'\\tmp\\'+file_tmp+".html" template_compiled = str(os.getcwd())+'\\tmp\\'+file_tmp+".opt.html" #Add array for Muncher list = [] list.append(('--css', cssGroupLinks.encode('UTF8'))) list.append(('--html',template)) #Run Muncher... config = Config() config.processArgs(list) muncher = Muncher(config) muncher.run() #Open Result with BeautifulSoup f=codecs.open(template_compiled, 'r', 'utf-8') #Load in a tmp_compiled tmp_compiled = BeautifulSoup(f.read()) #Show Minify and Muncher Site! response.set_data( tmp_compiled ) else: response.set_data( minify(response.get_data(as_text=True)) ) return response return response
def minifier(content): if isinstance(content, str): content = unicode(content, 'utf-8') return minify(content)
def response_minify(response): if env == 'prod' and response.content_type == u'text/html; charset=utf-8': response.set_data( minify(response.get_data(as_text=True))) return response return response
def ProfileDataStream(self, request_iterator, context): request_id = "none" builder = SchemaBuilder() builder.add_schema({"type": "object", "properties": {}}) error = domain_pb2.ProfilerError( type=domain_pb2.ProfilerError.Type.Value('UNKNOWN')) message = profiler_pb2.ProfileDataStreamResponse() total_records = 0 record_list = [] try: for record in request_iterator: total_records += 1 request_id = record.request_id if total_records == 1: logging.info( 'started profiling for request %s with config %s' % (request_id, self.config_path)) json_data = json.loads(record.json_data) record_list.append(json_data) for jd in record_list: builder.add_object(jd) data_frame = pd.DataFrame(json_normalize(record_list, sep='/')) profile = None report_length = 0 try: profile = run_profiler(data_frame) except FunctionTimedOut as te: err_msg = 'profile timeout for request_id %s after %ss data_frame shape (rows, cols): %s' % \ (request_id, te.timedOutAfter, data_frame.shape) logging.warning(err_msg) error = domain_pb2.ProfilerError( message=err_msg, type=domain_pb2.ProfilerError.Type.Value( 'PROFILE_EXCEPTION')) except Exception as e: logging.error('generic exception in timeout', e) error = domain_pb2.ProfilerError( message=str(e), type=domain_pb2.ProfilerError.Type.Value( 'PROFILE_EXCEPTION')) schema = builder.to_schema() if profile is not None: html = profile.to_html() html = minify(html, remove_all_empty_space=True, remove_comments=True) report_length = len(html) schema_json = json.dumps(schema) schema_length = len(schema_json) logging.info( 'profiling complete for request %s total_records: %s, schema_length: %s, report_length: %s' % (request_id, total_records, schema_length, report_length)) profile_stream = [] # The max message size of a GRPC call in bytes is 4194304. The header includes 5 bytes, 1 for # the compressed flag and 4 for the unsigned integer. Therefore should be 4194299 MAX_MESSAGE_SIZE = 4194299 if report_length == 0 or html is None: profile_stream.append('') elif report_length < MAX_MESSAGE_SIZE: profile_stream.append(html) else: last = 0 while last + MAX_MESSAGE_SIZE < report_length: profile_stream.append(html[last:last + MAX_MESSAGE_SIZE]) last = last + MAX_MESSAGE_SIZE profile_stream.append(html[last:report_length]) if error is not None and error.type != domain_pb2.ProfilerError.Type.Value( 'UNKNOWN'): message.meta.error.message = error.message message.meta.error.type = error.type message.meta.request_id = request_id message.meta.schema = schema_json message.meta.total_records = total_records message.meta.service_version = os.getenv( 'SDM_PROFILER_SERVICE_VERSION', 'default') message.meta.schema_byte_size = schema_length message.meta.profile_byte_size = report_length yield message for idx, profile_portion in enumerate(profile_stream): message = profiler_pb2.ProfileDataStreamResponse() message.profile = profile_portion yield message return except json.decoder.JSONDecodeError as e: first_chars = '><' if record is not None and record.json_data is not None: first_chars = '>' + record.json_data[0:10] + '<' err_msg = 'profiling failed for request %s with error %s %s, record nr: %s first 10 chars %s' % \ (request_id, type(e), e, total_records, first_chars) logging.error(err_msg) error = domain_pb2.ProfilerError( message=err_msg, type=domain_pb2.ProfilerError.Type.Value('UNKNOWN_ENCODING')) except Exception as e: logging.error('profiling failed for request %s with error %s' % (request_id, e)) error = domain_pb2.ProfilerError( message=str(e), type=domain_pb2.ProfilerError.Type.Value('NO_DATA')) message.meta.request_id = request_id message.meta.error.message = error.message message.meta.error.type = error.type yield message
def response_minify(response): """Minify HTML response to decrease bandwidth""" if response.content_type == u'text/html; charset=utf-8': response.set_data(minify(response.get_data(as_text=True))) return response
def minify_response(response): """Minify html response""" response.set_data(minify(response.get_data(as_text=True))) return response
def render(self, template, **kwargs): return minify(render_template(template, endpoint = request.endpoint, menu = self.make(), **kwargs))