我正在开始一个简单的小GUI来评分我学生的python源代码。有没有一种简单的方法可以自动格式化GUI中的python代码?例如,从某些编辑器中提取颜色格式?
我开始使用Python tkk(只是为了一点额外的python练习,我教它但不要使用它太多)但是如果在这方面更容易,我没有异议切换语言。
输出将是一个包含所有等级等的网页,但会显示使用Google Prettify的python代码(除非有人有更好的建议),所以我不需要保留配色方案,只是希望它显示为评分更容易。
非常感谢!
答案 0 :(得分:1)
正如@icktoofay所说,你可以使用Pygments。 PyQt / PiSide,PyGtk和wxPython都有WebKit小部件。这是使用PyGTK的一个例子(注意 - 不是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()
答案 1 :(得分:1)
ipython提供支持突出显示的qt console和web server interface。 ipython本身有很多features,可以更容易地使用python进行开发。为了让ipython的一切顺利,有一个很大的要求清单。大多数Linux发行版在其软件包管理存储库中都有ipython,并且this site提供了一个Windows安装程序。
bpython是另一个python iterpreter替代品。它提供语法突出显示,内联帮助和其他功能。 screenshots是了解外观的最佳场所。它比ipython更轻量级。
就个人而言,我会设置一个html笔记本服务器,并将其作为实验室/教室的一部分用于教授python。
对于问题的其他部分,在具有语法突出显示的网页上显示成绩;最简单的方法是使用众多static site generators中的一个。几乎所有支持语法突出显示插件。
答案 2 :(得分:1)
记得wxPython附带了SciTe:
#!/usr/bin/env python
import wx
from wx import stc
import keyword
class PyDialog(wx.Dialog):
def __init__(self):
wx.Dialog.__init__(self, None, -1, 'Python Code')
sizer = wx.BoxSizer(wx.VERTICAL)
self.stc = stc.StyledTextCtrl(self, -1)
self.stc.SetSizeHints(400, 400)
self.stc.SetLexer(stc.STC_LEX_PYTHON)
self.stc.SetKeyWords(0, " ".join(keyword.kwlist))
self.stc.SetMarginType(1, stc.STC_MARGIN_NUMBER)
# Python styles
self.stc.StyleSetSpec(wx.stc.STC_P_DEFAULT, 'fore:#000000')
# Comments
self.stc.StyleSetSpec(wx.stc.STC_P_COMMENTLINE, 'fore:#008000,back:#F0FFF0')
self.stc.StyleSetSpec(wx.stc.STC_P_COMMENTBLOCK, 'fore:#008000,back:#F0FFF0')
# Numbers
self.stc.StyleSetSpec(wx.stc.STC_P_NUMBER, 'fore:#008080')
# Strings and characters
self.stc.StyleSetSpec(wx.stc.STC_P_STRING, 'fore:#800080')
self.stc.StyleSetSpec(wx.stc.STC_P_CHARACTER, 'fore:#800080')
# Keywords
self.stc.StyleSetSpec(wx.stc.STC_P_WORD, 'fore:#000080,bold')
# Triple quotes
self.stc.StyleSetSpec(wx.stc.STC_P_TRIPLE, 'fore:#800080,back:#FFFFEA')
self.stc.StyleSetSpec(wx.stc.STC_P_TRIPLEDOUBLE, 'fore:#800080,back:#FFFFEA')
# Class names
self.stc.StyleSetSpec(wx.stc.STC_P_CLASSNAME, 'fore:#0000FF,bold')
# Function names
self.stc.StyleSetSpec(wx.stc.STC_P_DEFNAME, 'fore:#008080,bold')
# Operators
self.stc.StyleSetSpec(wx.stc.STC_P_OPERATOR, 'fore:#800000,bold')
# Identifiers. I leave this as not bold because everything seems
# to be an identifier if it doesn't match the above criterae
self.stc.StyleSetSpec(wx.stc.STC_P_IDENTIFIER, 'fore:#000000')
# Caret color
self.stc.SetCaretForeground("BLUE")
# Selection background
self.stc.SetSelBackground(1, '#66CCFF')
sizer.Add(self.stc, 0, wx.EXPAND)
button = wx.Button(self, -1, 'Open...')
self.Bind(wx.EVT_BUTTON, self.OnOpen, button)
sizer.Add(button)
self.SetSizer(sizer)
sizer.Fit(self)
def OnOpen(self, evt):
dlg = wx.FileDialog(
self,
message = 'Choose File',
wildcard = 'Python source (*.py)|*.py',
style = wx.OPEN)
if dlg.ShowModal() != wx.ID_OK:
return
with open(dlg.GetPath()) as fo:
self.stc.SetText(fo.read())
dlg.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
dlg = PyDialog()
with open(__file__) as fo:
dlg.stc.SetText(fo.read())
dlg.ShowModal()