from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters.html import HtmlFormatter code = ''' print("Hello, world!") ''' formatter = HtmlFormatter() highlighted_code = highlight(code, PythonLexer(), formatter) print(highlighted_code)
from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters.html import HtmlFormatter code = ''' def add_nums(num1, num2): total = num1 + num2 return total print(add_nums(5, 10)) ''' formatter = HtmlFormatter(style='friendly', full=True) highlighted_code = highlight(code, PythonLexer(), formatter) print(highlighted_code)This code uses HtmlFormatter to create an HTML-formatted output of a Python function that adds two numbers together. The code is highlighted in the friendly style, and the output includes all available formatting tags. Overall, pygments.formatters.html is a useful package/library for developers who want to create HTML-formatted outputs of their code, and HtmlFormatter provides many options for customization.