Best Python code snippet using autotest_python
json_html_formatter.py
Source:json_html_formatter.py
...37</body>38</html>39"""40class JsonHtmlFormatter(object):41 def _html_encode(self, value):42 if value is None:43 return ''44 return (str(value).replace('&', '&').replace('"', '"')45 .replace('<', '<').replace('>', '>'))46 def _decorate_with_span(self, value, className):47 return '<span class="%s">%s</span>' % (48 className, self._html_encode(value))49 # Convert a basic JSON datatype (number, string, boolean, null, object,50 # array) into an HTML fragment.51 def _value_to_html(self, value):52 if value is None:53 return self._decorate_with_span('null', 'null')54 elif isinstance(value, list):55 return self._array_to_html(value)56 elif isinstance(value, dict):57 return self._object_to_html(value)58 elif isinstance(value, bool):59 return self._decorate_with_span(str(value).lower(), 'bool')60 elif isinstance(value, (int, float)):61 return self._decorate_with_span(value, 'num')62 else:63 assert isinstance(value, basestring)64 return self._decorate_with_span('"%s"' % value, 'string')65 # Convert an array into an HTML fragment66 def _array_to_html(self, array):67 if not array:68 return '[ ]'69 output = ['[<ul class="array collapsible">']70 for value in array:71 output.append('<li>')72 output.append(self._value_to_html(value))73 output.append('</li>')74 output.append('</ul>]')75 return ''.join(output)76 def _link_href(self, href):77 if '?' in href:78 joiner = '&'79 else:80 joiner = '?'81 return href + joiner + 'alt=json-html'82 # Convert a JSON object to an HTML fragment83 def _object_to_html(self, json_object):84 if not json_object:85 return '{ }'86 output = ['{<ul class="obj collapsible">']87 for key, value in json_object.iteritems():88 assert isinstance(key, basestring)89 output.append('<li>')90 output.append('<span class="prop">%s</span>: '91 % self._html_encode(key))92 value_html = self._value_to_html(value)93 if key == 'href':94 assert isinstance(value, basestring)95 output.append('<a href="%s">%s</a>' % (self._link_href(value),96 value_html))97 else:98 output.append(value_html)99 output.append('</li>')100 output.append('</ul>}')101 return ''.join(output)102 # Convert a whole JSON object into a formatted HTML document.103 def json_to_html(self, json_value):104 return _HTML_DOCUMENT_TEMPLATE % self._value_to_html(json_value)105class JsonToHtmlMiddleware(object):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!