[英]Progress bar in Sublime Text with Python
我在MacOSX上使用强大的Sublime Text 3编辑器来运行python代码。 我想显示for循环的进度条,以及以下命令:
sys.stdout.write('\rProgress : %03.2f %%' % (100*float(i)/(N)))
sys.flush()
不会按预期( \\r
)清除输出窗口中先前打印的行,但会生成N
行:
Progress : 0.25 %
Progress : 0.50 %
Progress : 0.75 %
Progress : 1.00 %
Progress : 1.25 %
Progress : 1.50 %
Progress : 1.75 %
Progress : 2.00 %
Progress : 2.25 %
Progress : 2.50 %
...
这不是很好读 - 我得出结论,输出窗口可能是只读的。
有没有人建议改善Sublime Text中进度条的使用?
看一下sublime.py
我们看到flush
方法实际上什么也没做:
class _LogWriter:
def flush(self):
pass
def write(self, s):
sublime_api.log_message(s)
sys.stdout = _LogWriter()
sys.stderr = _LogWriter()
但是我不建议将控制台用于用户输出。 通常使用输出面板/视图或状态消息。
状态消息更易于使用,但功能较弱。 sergioFC在他的回答中证明了这一点。
这演示了如何使用输出面板。 它非常灵活,但您必须编写自己的文本命令来插入文本。 这是必要的,因为您需要一个编辑对象来更改视图的内容。
import sublime
import sublime_plugin
class MyInsertProgressBarCommand(sublime_plugin.TextCommand):
def run(self, edit, value):
view = self.view
width, _ = view.viewport_extent()
em_width = view.em_width()
# the number of columns are the width divided by the width of a char
# subtract two to add a little border
columns = int(width / em_width) - 2
# subtract two, because we surround it with [ and ]
bar_length = columns - 2
# calculate the size of the filled and the remaining part
filled_length = int(bar_length * value / 100)
remaining_length = bar_length - filled_length
# assemble the string for the progress bar
text = "[{0}{1}]\n".format("=" * filled_length, "." * remaining_length)
# add the text for the percentages
if value >= 100:
percentage_text = "finished!"
else:
percentage_text = "{:3.2f} %".format(value)
text += " " * (columns - len(percentage_text)) + percentage_text
# replace the content of the view
view.replace(edit, sublime.Region(0, view.size()), text)
# reset sels
view.sel().clear()
view.sel().add(sublime.Region(0, 0))
class ProgressBarCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.create_output_panel("progess_bar")
self.window.run_command("show_panel", {"panel": "output.progess_bar"})
def test_progress_bar():
import random
test_progress_bar.value += 2 * random.random()
if test_progress_bar.value >= 100:
self.finish_progress()
return
self.show_progress(test_progress_bar.value)
sublime.set_timeout(test_progress_bar, 100)
test_progress_bar.value = 0
sublime.set_timeout_async(test_progress_bar, 1)
def show_progress(self, progess):
view = self.window.find_output_panel("progess_bar")
view.run_command("my_insert_progress_bar", {"value": progess})
def finish_progress(self):
self.show_progress(100)
sublime.set_timeout(self._destroy, 5000)
def _destroy(self):
self.window.destroy_output_panel("progess_bar")
输出:
作为另一种替代方案, 您可以使用状态栏 。 设置状态栏消息时,将清除上一个文本。 包控件在安装包时也使用状态栏。
例:
import sublime, sublime_plugin
import time
class ExampleCommand(sublime_plugin.WindowCommand):
def run(self, args):
sublime.set_timeout_async(self.test,1)
def test(self):
i=80
while i <= 100:
sublime.status_message('%03.2f %%' % i)
time.sleep(0.15)
i+=0.25
sublime.status_message('100% Stackoverflow!')
您可以使用以下方法创建可视进度条:
(通过在Command Palette中键入Progress Bar Demo
来运行插件)
有一个css文件控制mdpopups
的样式。 由于某种原因, color
属性没有任何影响。
此外, mdpopups.show_popup
的location
参数取值为-1
,以便在插入符号位置设置弹出窗口。 否则,我不确定location
如何影响弹出窗口,因为它只需要一个整数值。
我在下面的帖子中询问过这些问题:
您可能正在寻找的是一种保持输出消耗多行的方法。 您可以打印\\b
(退格符)与以前打印的字符一样多次。 我写这个作为一个例子:
(Python 2.7.6)
from __future__ import print_function
import time, sys
for i in range(1, 6):
print(i, end='')
sys.stdout.flush()
time.sleep(0.5)
print('\b', end='')
尝试运行它,您可以根据自己的需要进行调整。
您可以使用进度条库。 位于: https : //pypi.python.org/pypi/progressbar/2.3-dev
您也可以从easy_install安装它,只需输入: easy_install progressbar
使用示例:
如果你想要简单的进度条,没有关于功能的信息:
from progressbar import *
from time import sleep
progress = ProgressBar()
for i in progress(range(80)):
sleep(0.01)
否则,如果您想要有关函数信息的进度条:
from progressbar import *
from time import sleep
widgets = ['Something: ', Percentage(), ' ', Bar(marker=RotatingMarker()),
' ', ETA(), ' ', FileTransferSpeed()]
pbar = ProgressBar(widgets=widgets, maxval=10000000).start()
for i in range(1000000):
# do something
pbar.update(10*i+1)
sleep(0.000001)
pbar.finish()
不幸的是,这在Sublime的输出面板中是不可能的。 该面板是不是真正的控制台或终端,和其他方面的差异不解释转义序列如间\\r
和\\b
( \\n
然而被正确地解释)。 如果要查看其工作原理,请安装PackageResourceViewer
,然后打开Packages/Default/exec.py
为了使其工作,您需要创建一个新的构建系统以在终端中运行它。 由于OS X的变幻莫测,您需要创建两个文件。 第一个是shell脚本:
#!/bin/sh
osascript -e '
on run parameters
tell application "Terminal"
activate
do script with command "/path/to/python " & parameters
end tell
end run
' $@
使用python
(或python3
)的实际路径更改/path/to
。 将它保存在PythonTerminal.sh
任何PythonTerminal.sh
。 接下来,选择Tools -> Build System -> New Build System
并粘贴以下内容:
{
"cmd": ["/bin/sh /path/to/Python3Terminal.sh \"$file\""],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"shell": true
}
再次,将/path/to
更改为PythonTerminal.sh
的实际路径。 将文件另存为Packages/User/PythonTerminal.sublime-build
(保存时应自动打开正确的目录)。
最后,选择Tools -> Build System -> PythonTerminal
,切换到你的Python文件,并与⌘ 乙建立。 将打开一个新的终端窗口,您的进度条应该会运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.