2017-10-20 71 views
0

我已经使用了散景生成400个图形并将它们保存到我的Mac本地驱动器上的400个html文件(file_1.html ... file_400.html)中。如何通过Safari浏览器和Python3高效查看数百个本地html文件

,我用于生成图表,并保存它低于

import numpy as np 

from bokeh.plotting import figure, output_file, save 

p = figure(plot_width=400, plot_height=400) 

x = np.arange(1, 1000) # all 400 graphs have the same x 
y1 = np.arange(1, 1000)*2 # different file can have different y 
p.line(x, y1, line_width=2) 

output_file('file_1.html') 
save(p) 

我需要逐一查看400的HTML文件中的代码的一个例子,以及我对仅在zoomed-根据每个图表,即每个图表的最后100个点。请注意,每张图中的曲线都必须由我来看(由于我的专业知识),所以我不能使用人工智能之类的东西来查看我的图。

我现在能做什么,就是:

  1. 打开文件夹包含这些400的HTML文件
  2. 双击一个文件,那么它将与Safari网页浏览器中打开
  3. 点击放大通过bokeh
  4. 定义-in按钮,找到最后的100点的区域,通过鼠标拖动矩形放大
  5. 关闭这个文件
  6. 再重复上述5个步骤399次。

这种方法非常耗时和无聊。

你有更好的方法去浏览所有这些文件吗?

一个首选功能是我可以在窗口中全部打开它们,它们会自动放大,我只需要点击键盘上的left-arrowright-arrow按钮即可浏览图形。

期待您的帮助和谢谢!

回答

0

好吧,让我们看看我们如何做到这一点。我的第一个想法是,这可以通过硒来完成。我会假设你以前没有用过它。简而言之,这是一种通过浏览器以编程方式进行操作的方式。

让我们开始吧!安装python库

pip install selenium 

您还需要安装geckodriver(我们将在本例中使用firefox)。如果你在osx上,你可以用brew来安装。

brew install geckodriver 

然后我们就可以开始编写我们的脚本来打开400个标签!它会打开你在本地的所有数字。我会告诉你如何弄清楚如何放大。硒的文档可以found here ->

(脚本使用Python 3,和pathlib只有在Python 3存在)

from pathlib import Path 
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.common.action_chains import ActionChains 

html_path = Path.cwd() 
browser = webdriver.Firefox() 

for no in range(1, 400): 
    (ActionChains(browser).key_down(Keys.CONTROL) 
          .send_keys('t') 
          .key_up(Keys.CONTROL) 
          .perform()) 
    file_path = html_path/'file_1.html' 
    browser.get('file://' + str(file_path)) 
1

这实际上似乎是一个完美的使用情况,您可以运行一个小背景虚化服务器应用程序本地。您可以将代码放入文件app.py,然后在命令行运行bokeh serve --show app.py

import numpy as np 
from bokeh.io import curdoc 
from bokeh.models import Button, ColumnDataSource, TextInput 
from bokeh.layouts import widgetbox, row 
from bokeh.plotting import figure 

current = 0 

x = np.linspace(0, 20, 500) 
y = np.sin(x)  
source = ColumnDataSource(data=dict(x=x, y=y)) 

plot = figure(x_range=(10,20), title="Plot 0") 
plot.line('x', 'y', source=source) 

def update_data(i): 
    global current 
    current = i 

    # compute new data or load from file, etc 
    source.data = dict(x=x, y = np.sin(x*(i+1))) 
    plot.title.text = "Plot %d" % i 

def update_range(attr, old, new): 
    plot.x_range.start = float(start.value) 
    plot.x_range.end = float(end.value) 

start = TextInput(title="start", value="10") 
start.on_change('value', update_range) 
end = TextInput(title="start", value="20") 
end.on_change('value', update_range) 

next = Button(label="next") 
next.on_click(lambda: update_data(current+1)) 
prev = Button(label="prev") 
prev.on_click(lambda: update_data(current-1)) 

curdoc().add_root(row(widgetbox(start, end, next, prev), plot)) 

这可以改进一些错误处理,也许一些额外的钟声和口哨声,但有希望证明。它产生以下互动应用程序:


enter image description here

+0

这是很多更优雅的解决方案。 – Jonathan

+0

Selenium确实是一个很好的工具(Bokeh在内部使用它来生成PNG输出,例如) – bigreddot

+0

@bigreddot感谢您的帮助。看起来不错,但在我的情况下,我还需要阅读html文件并放大。在你的代码中,图形只是在内存中创建的,而不是保存在文件中。 – aura

相关问题