Exemple #1
0
 def __init__(
         self,
         stripnl=False,
         stripall=False,
         ensurenl=True,
         tabsize=tabideal,
         encoding=default_encoding,
         python3=True,
         **options  # 2021-02-04 patch
 ):
     """Initialize the Python console syntax highlighter."""
     PythonConsoleLexer.__init__(
         self,
         stripnl=stripnl,
         stripall=stripall,
         ensurenl=ensurenl,
         tabsize=tabsize,
         encoding=default_encoding,
         python3=python3,
         **options  # 2021-02-04 patch
     )
     (self.original_add_filter, self.add_filter) = (
         self.add_filter,
         lenient_add_filter,
     )
Exemple #2
0
    def __init__(self, filename, output=None, width=80, colors=True,
                 animation=True, typing_delay=40, logging=False):
        if output is None:
            self.output = sys.stdout
        else:
            self.output = output

        self.logging = logging
        if logging:
            time = datetime.now().strftime('-%Y-%m-%d')
            log_name = os.path.splitext(filename)[0] + time + '.log'
            self.logger = open(log_name, 'at')
            self.logger.write('\n')
            bar = '=' * 34
            self.log(bar, '=====  AutoPython initiated  =====', bar)

        self.colors = colors
        self.animation = animation
        self.typing_delay = typing_delay

        self.lexer = PythonConsoleLexer(python3=True)
        self.width = self.current_width = width - 1
        
        self.ps1 = self.hl_ps1 = '>>> '
        self.ps2 = self.hl_ps2 = '... '

        self.colorscheme = TERMINAL_COLORS
        if self.colors:
            self.hl_ps1 = ansiformat(self.colorscheme[Token.Generic.Prompt], self.ps1)
            self.hl_ps2 = ansiformat(self.colorscheme[Token.Generic.Prompt], self.ps2)

        self.reset_interpreter()
        self.load_file(filename)
Exemple #3
0
    def run(self, global_environment):
        results = []
        for i, (max_points, test) in enumerate(self.tests):
            name = "{} test #{}".format(self.name, i + 1)
            passed, result = run_doctest(name, test, global_environment)
            if not passed:
                results.append((
                    False,
                    (0, max_points),
                    OKTest.result_fail_template.render(
                        name=name,
                        test_code=highlight(
                            test,
                            PythonConsoleLexer(),
                            HtmlFormatter(noclasses=True),
                        ),
                        test_result=result,
                    ),
                ))
            else:
                results.append((
                    True,
                    (max_points, max_points),
                    OKTest.result_pass_template.render(name=name),
                ))

        return results
Exemple #4
0
 def _repr_html_(self):
     if self.passed:
         return OKTest.html_result_pass_template.render(name=self.name)
     else:
         return OKTest.html_result_fail_template.render(
             name=self.name,
             test_code=highlight(self.failed_test, PythonConsoleLexer(),
                                 HtmlFormatter(noclasses=True)),
             test_result=self.result)
Exemple #5
0
 def run(self, global_environment):
     for i, t in enumerate(self.tests):
         passed, result = run_doctest(self.name + ' ' + str(i), t, global_environment)
         if not passed:
             return False, OKTest.result_fail_template.render(
                 name=self.name,
                 test_code=highlight(t, PythonConsoleLexer(), HtmlFormatter(noclasses=True)),
                 test_result=result
             )
     return True, OKTest.result_pass_template.render(name=self.name)
    def _info(self, type_, value, tb):
        if not self._exception_in_progress.acquire(False):
            # Exceptions have piled up, so we use the default exception
            # handler for such exceptions
            self._excepthook_save(type_, value, tb)
            return
        trace = StringIO()
        traceback.print_exception(type_, value, tb, None, trace)

        print highlight(trace.getvalue(), PythonConsoleLexer(),
            TerminalFormatter())
        self._exception_in_progress.release()
Exemple #7
0
def main():
    formatter = HtmlFormatter(full=False, style='monokai')

    html = []
    with open('out.html', 'w') as f:
        html.append('<link rel="stylesheet" type="text/css" href="style.css">')
        html.append(MARKDOWN)
        html.append(highlight(CODE, ScalaLexer(), formatter))
        html.append(highlight(RESULT, PythonConsoleLexer(), formatter))
        html.append(MARKDOWN1)
        f.write("\n\n".join(html))

    with open('style.css', 'w') as f:
        f.write(formatter.get_style_defs('body'))
Exemple #8
0
    def __init__(self, *args, locals=None, **kwargs):
        super().__init__(*args,
                         background_color=(0, 0, 0, 1),
                         cursor_color=HIGHLIGHTED_EDGE,
                         **kwargs)
        self.style = GraphvyStyle
        self.lexer = PythonConsoleLexer()
        self.font_name = './UbuntuMono-R.ttf'

        self.history = deque([''])
        self.console = Console(self, locals)
        self.input_handler = InputHandler(self)

        self.text = (
            f'Python {sys.version.splitlines()[0]}\n'
            'Welcome to the GraphvyConsole -- `G` references current graph.\n')
        self.prompt()
def test_embedded_lexer():
    # Latex surrounded by '|' should be Escaped
    lexer = LatexEmbeddedLexer('|', '|', PythonConsoleLexer())

    # similar to gh-1516
    src = dedent("""\
    >>> x = 1
    >>> y = mul(x, |$z^2$|)  # these |pipes| are untouched
    >>> y
    |$1 + z^2$|""")

    assert list(lexer.get_tokens(src)) == [(Token.Name, ''),
        (Token.Generic.Prompt, '>>> '),
        (Token.Name, 'x'),
        (Token.Text, ' '),
        (Token.Operator, '='),
        (Token.Text, ' '),
        (Token.Literal.Number.Integer, '1'),
        (Token.Text, '\n'),
        (Token.Generic.Prompt, '>>> '),
        (Token.Text, ''),
        (Token.Name, 'y'),
        (Token.Text, ' '),
        (Token.Operator, '='),
        (Token.Text, ' '),
        (Token.Name, 'mul'),
        (Token.Punctuation, '('),
        (Token.Name, 'x'),
        (Token.Punctuation, ','),
        (Token.Text, ' '),
        (Token.Escape, '$z^2$'),
        (Token.Text, ''),
        (Token.Punctuation, ')'),
        (Token.Text, '  '),
        (Token.Comment.Single, '# these |pipes| are untouched'),  # note: not Token.Escape
        (Token.Text, '\n'),
        (Token.Generic.Prompt, '>>> '),
        (Token.Text, ''),
        (Token.Name, 'y'),
        (Token.Text, '\n'),
        (Token.Escape, '$1 + z^2$'),
        (Token.Text, ''),
        (Token.Generic.Output, '\n'),
    ]
Exemple #10
0
def para2Html(formatter, d_para):
    lang = d_para['config']['editorSetting']['language']
    html = []
    if d_para['text']:
        if lang == 'markdown':
            html.extend([
                d_msg['data']
                for d_msg in d_para.get('results', {}).get('msg', '')
            ])
        else:
            html.append(
                highlight(d_para['text'], get_lexer_by_name(lang,
                                                            stripall=True),
                          formatter))
            for data in filter(
                    None,
                    map(lambda d_msg: d_msg['data'],
                        d_para.get('results', {}).get('msg', ''))):
                html.append(highlight(data, PythonConsoleLexer(), formatter))
    return html
def para2Html(formatter, d_para):
    html = []
    if d_para['text']:
        lang = d_para.get('config', {}).get('editorSetting',
                                            {}).get('language', 'text')
        l_d_msg = d_para.get('results', {}).get('msg', [])
        if lang == 'markdown':
            html.extend([d_msg['data'] for d_msg in l_d_msg])
        else:
            html.append(
                highlight(d_para['text'], get_lexer_by_name(lang,
                                                            stripall=True),
                          formatter))
            for (data, data_type) in map(
                    lambda d_msg: (d_msg['data'], d_msg['type']), l_d_msg):
                if data:
                    if data_type == 'HTML':
                        html.append(data)
                    else:
                        html.append(
                            highlight(data, PythonConsoleLexer(), formatter))
    return html
Exemple #12
0
 def run(self, global_environment):
     result_dict = defaultdict(list)
     num_failed = 0
     num_total = len(self.tests)
     for i, t in enumerate(self.tests):
         passed, result, stat = run_doctest(self.name + ' ' + str(i), t,
                                            global_environment)
         result_dict['passed'].append(passed)
         if not passed:
             result_dict['result'].append(
                 OKTest.result_fail_template.render(
                     name=self.name,
                     test_code=highlight(t, PythonConsoleLexer(),
                                         HtmlFormatter(noclasses=True)),
                     test_result=result))
             num_failed += 1
     all_passed = all(result_dict['passed'])
     display_ = None
     if all_passed:
         display_ = OKTest.result_pass_template.render(name=self.name)
     else:
         display_ = "\n\n".join(result_dict['result'])
     return all_passed, display_, (num_total - num_failed) / num_total
Exemple #13
0
def makeLexer(file):
    if file.startswith('>>>'):
        return PythonConsoleLexer()
    else:
        return PythonLexer()
Exemple #14
0
from sphinx.util import logging
from sphinx.util.pycompat import htmlescape
from sphinx.util.texescape import tex_hl_escape_map_new

if False:
    # For type annotation
    from typing import Any, Dict  # NOQA
    from pygments.formatter import Formatter  # NOQA

logger = logging.getLogger(__name__)

lexers = dict(
    none=TextLexer(stripnl=False),
    python=PythonLexer(stripnl=False),
    python3=Python3Lexer(stripnl=False),
    pycon=PythonConsoleLexer(stripnl=False),
    pycon3=PythonConsoleLexer(python3=True, stripnl=False),
    rest=RstLexer(stripnl=False),
    c=CLexer(stripnl=False),
)  # type: Dict[unicode, Lexer]
for _lexer in lexers.values():
    _lexer.add_filter('raiseonerror')

escape_hl_chars = {
    ord(u'\\'): u'\\PYGZbs{}',
    ord(u'{'): u'\\PYGZob{}',
    ord(u'}'): u'\\PYGZcb{}'
}

# used if Pygments is available
# use textcomp quote to get a true single quote
Exemple #15
0
class Presenter(object):
    READY_TO_TYPE, READY_TO_EXECUTE, READY_TO_FAIL = range(3)

    KEY_PREV = (console.PGUP,) + lower_upper_key('p')
    KEY_NEXT = (console.PGDN, ' ', '\n', '\r')
    KEY_AGAIN = lower_upper_key('r')
    KEY_GOTO = lower_upper_key('g')
    KEY_SHELL = lower_upper_key('s')
    KEY_EXIT = lower_upper_key('q')


    def __init__(self, filename, output=None, width=80, colors=True,
                 animation=True, typing_delay=40, logging=False):
        if output is None:
            self.output = sys.stdout
        else:
            self.output = output

        self.logging = logging
        if logging:
            time = datetime.now().strftime('-%Y-%m-%d')
            log_name = os.path.splitext(filename)[0] + time + '.log'
            self.logger = open(log_name, 'at')
            self.logger.write('\n')
            bar = '=' * 34
            self.log(bar, '=====  AutoPython initiated  =====', bar)

        self.colors = colors
        self.animation = animation
        self.typing_delay = typing_delay

        self.lexer = PythonConsoleLexer(python3=True)
        self.width = self.current_width = width - 1
        
        self.ps1 = self.hl_ps1 = '>>> '
        self.ps2 = self.hl_ps2 = '... '

        self.colorscheme = TERMINAL_COLORS
        if self.colors:
            self.hl_ps1 = ansiformat(self.colorscheme[Token.Generic.Prompt], self.ps1)
            self.hl_ps2 = ansiformat(self.colorscheme[Token.Generic.Prompt], self.ps2)

        self.reset_interpreter()
        self.load_file(filename)


    def reset_interpreter(self):
        self.interpreter = HighlightingInterpreter() if self.colors else PresenterInterpreter()
        self.ns = self.interpreter.locals
        self.ns.update(PRESENTER=self)
        self._code_to_execute = None
        self._code_to_fail = None
        self.state = Presenter.READY_TO_TYPE
        self.index = 0


    def load_file(self, filename):
        self.statements = script_parser.parse_file(filename)


    def log(self, message, *rest):
        if self.logging:
            timestamp = datetime.now().strftime('[%Y-%m-%d %H:%M:%S]')
            print(timestamp, message, file=self.logger)
            indent = ' ' * len(timestamp)
            for line in rest:
                print(indent, line.rstrip(), file=self.logger)


    def begin(self):
        cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
        self.write('AutoPython %s on %s\n%s\n' % (sys.version, sys.platform, cprt))
        self.index = 0
        self.prompt()
        self.log('In the beginning...')


    def prompt(self):
        self.write(self.hl_ps1)
        self.state = Presenter.READY_TO_TYPE


    def cancel(self):
        self.write(' ^C\n', ansiformat('*red*', 'KeyboardInterrupt') if self.colors else 'KeyboardInterrupt', '\n')
        self.prompt()


    def end(self):
        if self.state != Presenter.READY_TO_TYPE:
            self.cancel()
        self.show_code((self.ps1, 'exit()'))
        self.write('\n')

        if self.logging:
            pending = len(self.statements) - self.index
            if pending > 0:
                reason = 'Quiting with {} more statement{} to go.'.format(pending, 's' if pending > 1 else '')
            else:
                reason = 'The End.'

            self.log(reason)
            self.logger.close()


    def prev(self):
        if self.index > 1 or (self.index == 1 and self.state == Presenter.READY_TO_TYPE):
            if self.state != Presenter.READY_TO_TYPE:
                self.cancel()
                self.index -= 2
            else:
                self.index -= 1

            self.log('Going back to previous statement.')
            return self.next()
        else:
            return self.index < len(self.statements)


    def next(self):
        if self.state == Presenter.READY_TO_TYPE and self.index < len(self.statements):
            line_number, statement, lines, statement_first_line, code = self.statements[self.index]
            self.index += 1
            self.log('On statement {} (line {}):'.format(self.index, line_number), *statement.splitlines())
            self.show_code(lines, self.index, statement_first_line)

            if code is None:
                self._code_to_fail = statement
                self.state = Presenter.READY_TO_FAIL
            else:
                self._code_to_execute = code
                self.state = Presenter.READY_TO_EXECUTE

        elif self.state == Presenter.READY_TO_EXECUTE:
            line_number, *_ = self.statements[self.index-1]
            self.log('Executing statement {} (line {}):'.format(self.index, line_number))
            
            self.write('\n')
            if self._code_to_execute is not None:
                self.interpreter.runcode(self._code_to_execute)
                self._code_to_execute = None
            self.prompt()

        elif self.state == Presenter.READY_TO_FAIL:
            line_number, *_ = self.statements[self.index-1]
            self.log('Raising exception on statement {} (line {}):'.format(self.index, line_number))
            
            self.write('\n')
            if self._code_to_fail is not None:
                self.interpreter.compilesource(self._code_to_fail)
                self._code_to_fail = None
            self.prompt()

        return self.state != Presenter.READY_TO_TYPE or self.index < len(self.statements)


    def go_to(self, n):
        if self.state != Presenter.READY_TO_TYPE:
            self.next()

        if n < 0:
            n = len(self.statements) + n + 1


        if 1 <= n <= len(self.statements):
            self.log('Jumping to statement {} (line {}).'.format(n, self.statements[n-1][0]))

            n -= 1
            if n < self.index:
                self.reset_interpreter()
            elif self.state != Presenter.READY_TO_TYPE:
                self.next()

            if n > 0:
                old_anim = self.animation
                self.animation = False
                try:
                    while self.index < n:
                        self.next()
                    self.next()
                finally:
                    self.animation = old_anim

        return self.next()


    def interact(self):
        if self.state != Presenter.READY_TO_TYPE:
            self.cancel()
        self.write('\r')

        self.log('Entering interactive mode.')

        if self.colors:
            ps1 = ansiformat('*green*', self.ps1)
            ps2 = ansiformat('*green*', self.ps2)
        else:
            ps1 = self.ps1
            ps2 = self.ps2

        lines = []
        need_more = False
        while True:
            try:
                try:
                    self.write(ps2 if need_more else ps1)
                    line = input()
                    lines.append(line)
                except EOFError:
                    break
                else:
                    source = '\n'.join(lines)
                    need_more = self.interpreter.runsource(source)
                    if not need_more:
                        self.log('Executing new statement:', *lines)
                        lines = []
            except KeyboardInterrupt:
                self.write('\n', ansiformat('*red*', 'KeyboardInterrupt') if self.colors else 'KeyboardInterrupt', '\n')
                lines = []
                need_more = False

        self.log('Leaving interactive mode.')
        self.write('\r', self.hl_ps1)


    def write(self, text, *more_text):
        self.output.write(text)
        for t in more_text:
            self.output.write(t)
        self.output.flush()


    def show_code(self, code, index=None, index_line=-1):
        index_str = '  ({})'.format(index) if index_line >= 0 else ''
        default_width = self.width
        reduced_width = default_width - len(index_str)

        def process_code():
            line = col = 0
            if index_line == 0:
                width = reduced_width
            else:
                width = default_width
                yield line, col, Token.Text, '\r'

            for ttype, value in self.lexer.get_tokens(''.join(code)):
                if value == '':
                    continue
                elif value == '\n':
                    yield line, col, ttype, value

                    line += 1
                    col = 0
                    width = reduced_width if line == index_line else default_width
                else:
                    new_col = col + len(value)
                    while new_col > width:
                        extra = new_col - width
                        yield line, col, ttype, value[:-extra]
                        yield line, new_col - extra, Token.Text, '\n'
                        value = value[-extra:]
                        col = 0
                        new_col = len(value)
                        width = default_width

                    yield line, col, ttype, value
                    col = new_col

        def add_index_and_animations(tokens):
            index_not_shown = True
            for line, col, ttype, value in tokens:
                if index_not_shown and line == index_line:
                    blanks = ' ' * (reduced_width - col)
                    if line == 0:
                        blanks = blanks[:-4]
                    if blanks:
                        yield Token.Text, blanks, line, False
                    yield Token.Generic.Strong, index_str, line, False
                    yield Token.Text, '\r', line, False
                    index_not_shown = False

                anim = not (ttype is Token.Generic.Prompt or
                            (len(value) > 1 and value.isspace()))
                yield ttype, value, line, anim

        def strip_last_newline(tokens):
            iterator = iter(tokens)
            last = next(iterator)
            for token in iterator:
                yield last
                last = token
            if last[1] != '\n':
                yield last

        def colorize(tokens):
            if self.colors:
                for ttype, value, line, anim in tokens:
                    while ttype:
                        color = self.colorscheme.get(ttype)
                        if color:
                            yield value, line, anim, color
                            break
                        ttype = ttype[:-1]
                    else:
                        yield value, line, anim, None
            else:
                for ttype, value, line, anim in tokens:
                    yield value, line, anim, None

        def show(tokens):
            anim = self.animation
            delay = self.typing_delay / 1000

            for text, line, simulate_typing, color in tokens:
                if color is not None:
                    on, off = ansiformat(color, '|').split('|')
                    self.write(on)

                if anim and simulate_typing:
                    for char in text:
                        self.write(char)
                        if not char.isspace():
                            time.sleep(delay * (0.5 + random.random()))
                else:
                    self.write(text)

                if color is not None:
                    self.write(off)

        console.disable_echo()
        show(colorize(strip_last_newline(add_index_and_animations(process_code()))))
        console.enable_echo()


    def run(self):
        try:
            self.begin()

            more_sentences = True
            while True:
                key = console.getch()
                # print('Key code:', repr(key))
                if key in Presenter.KEY_PREV:
                    more_sentences = self.prev()

                elif key in Presenter.KEY_NEXT:
                    if more_sentences:
                        more_sentences = self.next()
                    else:
                        break

                elif key in Presenter.KEY_AGAIN:
                    more_sentences = self.again()

                elif key in Presenter.KEY_GOTO:
                    if self.state != Presenter.READY_TO_TYPE:
                        self.next()

                    try:
                        self.write('\n\n')
                        if self.colors:
                            self.write(colorama.Fore.GREEN, colorama.Style.BRIGHT)
                        self.write('Go to: ')
                        sentence = input()
                    finally:
                        if self.colors:
                            self.write(colorama.Style.RESET_ALL)
                        self.write('\n')
                        self.prompt()

                    try:
                        more_sentences = self.go_to(int(sentence))
                    except ValueError:
                        pass

                elif key in Presenter.KEY_SHELL:
                    self.interact()

                elif key in Presenter.KEY_EXIT:
                    break

            self.end()
        finally:
            if self.colors:
                self.write(colorama.Style.RESET_ALL)
Exemple #16
0
from sphinx.util.texescape import tex_hl_escape_map_new

if False:
    # For type annotation
    from typing import Any, Dict  # NOQA
    from pygments.formatter import Formatter  # NOQA
    from sphinx.util.typing import unicode  # NOQA


logger = logging.getLogger(__name__)

lexers = {
    'none': TextLexer(stripnl=False),
    'python': PythonLexer(stripnl=False),
    'python3': Python3Lexer(stripnl=False),
    'pycon': PythonConsoleLexer(stripnl=False),
    'pycon3': PythonConsoleLexer(python3=True, stripnl=False),
    'rest': RstLexer(stripnl=False),
    'c': CLexer(stripnl=False),
}  # type: Dict[unicode, Lexer]
for _lexer in lexers.values():
    _lexer.add_filter('raiseonerror')


escape_hl_chars = {ord(u'\\'): u'\\PYGZbs{}',
                   ord(u'{'): u'\\PYGZob{}',
                   ord(u'}'): u'\\PYGZcb{}'}

# used if Pygments is available
# use textcomp quote to get a true single quote
_LATEX_ADD_STYLES = r'''
Exemple #17
0
        """

        background_color = '#eeffcc'
        default_style = ''

        styles = FriendlyStyle.styles
        styles.update({
            Generic.Output: 'italic #333',
            Comment: 'italic #408090',
        })

    lexers = defaultdict(
        TextLexer,
        none=TextLexer(),
        python=PythonLexer(),
        pycon=PythonConsoleLexer(),
        rest=RstLexer(),
        c=CLexer(),
    )
    for _lexer in lexers.values():
        _lexer.add_filter('raiseonerror')

    fmter = HtmlFormatter(style=PythonDocStyle)


def highlight_block(source, lang):
    if not pygments:
        return '<pre>' + cgi.escape(source) + '</pre>\n'
    if lang == 'python':
        if source.startswith('>>>'):
            lexer = lexers['pycon']
Exemple #18
0
1
2
3
>>> def foo(a, b):
...     pass
... 
>>> class Foo(object):
...     BAR = 12
...
...     def bar(self):
...             pass
>>> @decorated
... def bar(a, b, c):
...     numbers = [1, 2, 3, 0.2, 0x12]
...     x = sorted(numbers)
... 
...     raise AttributeError('string' + "string")
... 
...     for x in range(1, 10):
...         while(True):
...             break
... 
"""

print()
print(highlight(TEST, PythonConsoleLexer(), TerminalFormatter()))
print()
print(40*'-')
print()
print(highlight(TEST, PythonConsoleLexer(), Terminal256Formatter()))
Exemple #19
0
 def __init__(self, stripnl=False, stripall=False, ensurenl=True, tabsize=tabideal, encoding=default_encoding, python3=True):
     """Initialize the Python console syntax highlighter."""
     PythonConsoleLexer.__init__(self, stripnl=stripnl, stripall=stripall, ensurenl=ensurenl, tabsize=tabsize, encoding=default_encoding, python3=python3)
     self.original_add_filter, self.add_filter = self.add_filter, lenient_add_filter
Exemple #20
0
from sphinx.ext import doctest

from pygments import highlight
from pygments.lexers import PythonLexer, PythonConsoleLexer, CLexer, \
    TextLexer, RstLexer
from pygments.lexers import get_lexer_by_name, guess_lexer
from pygments.formatters import HtmlFormatter, LatexFormatter
from pygments.filters import ErrorToken
from pygments.styles import get_style_by_name
from pygments.util import ClassNotFound
from sphinx.pygments_styles import SphinxStyle, NoneStyle

lexers = dict(
    none=TextLexer(),
    python=PythonLexer(),
    pycon=PythonConsoleLexer(),
    pycon3=PythonConsoleLexer(python3=True),
    rest=RstLexer(),
    c=CLexer(),
)
for _lexer in lexers.values():
    _lexer.add_filter('raiseonerror')

escape_hl_chars = {
    ord(u'\\'): u'\\PYGZbs{}',
    ord(u'{'): u'\\PYGZob{}',
    ord(u'}'): u'\\PYGZcb{}'
}

# used if Pygments is available
# use textcomp quote to get a true single quote
Exemple #21
0
# -*- coding: utf-8 -*-

import codecs

from pygments import highlight
from pygments.lexers import (PythonLexer, PythonConsoleLexer,
                             PythonTracebackLexer)
from pygments.formatters import HtmlFormatter

encoding = 'utf-8'
input_filename = 'code1.py'
output_filename = 'snippet.html'

with codecs.open(input_filename, 'r', encoding=encoding) as input_file:
    code = input_file.read()

lexer = PythonConsoleLexer(encoding=encoding, stripall=True)

formatter = HtmlFormatter(
    # linenos=True,
    noclasses=True,
    prestyles="padding: 20px 20px 20px 20px",
    # cssstyles="padding: 20px 20px 20px 20px",
    # hl_lines=[1, 7],
    # cssclass="source",
    style='monokai')

with codecs.open(output_filename, 'w', encoding=encoding) as output_file:
    highlight(code, lexer, formatter, outfile=output_file)