Displaying Pretty Python Code in Python (or Other) GUI
Question: I'm starting a simple little GUI for grading my student's python source code.
Is there an easy way to automatically format display of the python code within
a GUI? For example, pulling the color formatting from some editor?

I've started with Python tkk (just for a little extra python practice, I teach
it but don't use it much) but I have no objections switching languages if it
is easier in this aspect.

The output will a webpage with all the grades etc, but will show the python
code using Google Prettify (unless someone has a better suggestion), so I
don't need to keep the color scheme, just want it displayed to make grading
easier.

Many thanks!


Answer: As @icktoofay said, you can use Pygments. PyQt/PiSide, PyGtk and wxPython all
have WebKit widgets. Here's an example using PyGTK (note - not an expert in
PyGtk):

    
    
    #!/usr/bin/env python
    '''Example on using Pygments and gtk/webkit'''
    
    from pygments import highlight
    from pygments.lexers import PythonLexer
    from pygments.formatters import HtmlFormatter
    
    import gtk
    import webkit
    
    def gen_html(path):
        '''Generate HTML for Python file with embedded CSS.'''
        with open(path) as fo:
            code = fo.read()
    
        formatter = HtmlFormatter(linenos='table')
        css = formatter.get_style_defs()
        div = highlight(code, PythonLexer(), formatter)
    
        return '''<html>
            <head><style>{}</style></head>
            <body><div>{}</div></body>
            </html>'''.format(css, div)
    
    def get_file():
        '''Get file from user.'''
        dlg = gtk.FileChooserDialog(
            title=None,action=gtk.FILE_CHOOSER_ACTION_OPEN,
            buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
        dlg.set_default_response(gtk.RESPONSE_OK)
    
        # Only Python files
        filter = gtk.FileFilter()
        filter.set_name("Python files")
        filter.add_pattern("*.py")
        dlg.add_filter(filter)
    
        path = dlg.get_filename() if dlg.run() == gtk.RESPONSE_OK else None
        dlg.destroy()
        return path
    
    def load(view):
        '''Load a new file'''
        path = get_file()
        if not path:
            return
    
        html = gen_html(path)
        with open('/tmp/src.html', 'w') as fo:
            fo.write(html)
        view.load_html_string(html, '/')
    
    
    if __name__ == '__main__':
        box = gtk.VBox()
        # WebKit view in scroll
        view = webkit.WebView()
        sw = gtk.ScrolledWindow()
        sw.add(view)
        sw.set_size_request(600, 400)
        box.pack_start(sw)
    
        # Open file
        btn = gtk.Button('Open...')
        btn.connect('clicked', lambda e: load(view))
        box.pack_start(btn, False, False, 0)
    
        # Quit
        btn = gtk.Button('Quit')
        btn.connect('clicked', lambda e: gtk.main_quit())
        box.pack_start(btn, False, False, 0)
    
        # Main window
        win = gtk.Window(gtk.WINDOW_TOPLEVEL)
        win.add(box)
        win.show_all()
    
        gtk.main()
    

