imported mpl_figure_editor from traits tutorial
[BS_AY250.git] / week6 / week6.py
blob19f426a22bfd7b2e03baa64dee9aad9065be6d55
1 #!/usr/bin/env python
3 import urllib
5 from enthought.traits.api import HasTraits,Str,Float,Button
6 from enthought.traits.ui.api import View,Item,ButtonEditor
7 from yahoo.search.image import ImageSearch
9 import pylab as pl
11 def yahoosearch(query):
12 ''' Yahoo searches for an image and returns a URL, or an empty string if something goes wrong '''
13 try:
14 search = ImageSearch(app_id="junk",query=query)
15 search.results = 1
16 for res in search.parse_results():
17 return res.Url
18 except:
19 pass
21 return ""
23 def fetchlink(url):
24 ''' Fetch file from link and return path to local file '''
25 return urllib.urlretrieve(url)[0]
27 class ImgSearch(HasTraits):
28 query = Str("Enter search")
29 run_query = Button()
30 url = Str
31 localfile = Str
32 def _run_query_fired(self):
33 self.url = yahoosearch(self.query)
34 self.localfile = fetchlink(self.url)
36 pl.open(self.localfile)
38 view = View('query','url','localfile',Item('run_query',show_label=False))
40 c = ImgSearch()
41 c.configure_traits()