Growing up, I have always found the universe and space in general to be exciting. It is fun to dream about what worlds remain unexplored. I also enjoy seeing photos from other worlds or thinking about the vastness of space. What does this have to do with Python though? Well, the National Aeronautics and Space Administration (NASA) has a web API that allows you to search their image library.
You can read all about it on their website.
The NASA website recommends getting an Application Programming Interface (API) key. If you go to that website, the form that you will fill out is nice and short.
Technically, you do not need an API key to make requests against NASA’s services. However they do have rate limiting in place for developers who access their site without an API key. Even with a key, you are limited to a default of 1000 requests per hour. If you go over your allocation, you will be temporarily blocked from making requests. You can contact NASA to request a higher rate limit though.
Interestingly, the documentation doesn’t really say how many requests you can make without an API key.
The API documentation disagrees with NASA’s Image API documentation about which endpoints to hit, which makes working with their website a bit confusing.
For example, you will see the API documentation talking about this URL:
- https://api.nasa.gov/planetary/apod?api_key=API_KEY_GOES_HERE
But in the Image API documentation, the API root is:
- https://images-api.nasa.gov
For the purposes of this tutorial, you will be using the latter.
This article is adapted from my book: Creating GUI Applications with wxPython |
Using NASA’s API
When you start out using an unfamiliar API, it is always best to begin by reading the documentation for that interface. Another approach would be to do a quick Internet search and see if there is a Python package that wraps your target API. Unfortunately, there does not seem to be any maintained NASA libraries for Python. When this happens, you get to create your own.
To get started, try reading the NASA Images API document.
Their API documentation isn’t very long, so it shouldn’t take you very long to read or at least skim it.
The next step is to take that information and try playing around with their API.
Here are the first few lines of an experiment at accessing their API:
# simple_api_request.py import requests from urllib.parse import urlencode, quote_plus base_url = 'https://images-api.nasa.gov/search' search_term = 'apollo 11' desc = 'moon landing' media = 'image' query = {'q': search_term, 'description': desc, 'media_type': media} full_url = base_url + '?' + urlencode(query, quote_via=quote_plus) r = requests.get(full_url) data = r.json()
If you run this in a debugger, you can print out the JSON that is returned.
Here is a snippet of what was returned:
'items': [{'data': [{'center': 'HQ', 'date_created': '2009-07-18T00:00:00Z', 'description': 'On the eve of the ' 'fortieth anniversary of ' "Apollo 11's first human " 'landing on the Moon, ' 'Apollo 11 crew member, ' 'Buzz Aldrin speaks during ' 'a lecture in honor of ' 'Apollo 11 at the National ' 'Air and Space Museum in ' 'Washington, Sunday, July ' '19, 2009. Guest speakers ' 'included Former NASA ' 'Astronaut and U.S. ' 'Senator John Glenn, NASA ' 'Mission Control creator ' 'and former NASA Johnson ' 'Space Center director ' 'Chris Kraft and the crew ' 'of Apollo 11. Photo ' 'Credit: (NASA/Bill ' 'Ingalls)', 'keywords': ['Apollo 11', 'Apollo 40th Anniversary', 'Buzz Aldrin', 'National Air and Space ' 'Museum (NASM)', 'Washington, DC'], 'location': 'National Air and Space ' 'Museum', 'media_type': 'image', 'nasa_id': '200907190008HQ', 'photographer': 'NASA/Bill Ingalls', 'title': 'Glenn Lecture With Crew of ' 'Apollo 11'}], 'href': 'https://images-assets.nasa.gov/image/200907190008HQ/collection.json', 'links': [{'href': 'https://images-assets.nasa.gov/image/200907190008HQ/200907190008HQ~thumb.jpg', 'rel': 'preview', 'render': 'image'}]}
Now that you know what the format of the JSON is, you can try parsing it a bit.
Let’s add the following lines of code to your Python script:
item = data['collection']['items'][0] nasa_id = item['data'][0]['nasa_id'] asset_url = 'https://images-api.nasa.gov/asset/' + nasa_id image_request = requests.get(asset_url) image_json = image_request.json() image_urls = [url['href'] for url in image_json['collection']['items']] print(image_urls)
This will extract the first item in the list of items from the JSON response. Then you can extract the nasa_id, which is required to get all the images associated with this particular result. Now you can add that nasa_id to a new URL end point and make a new request.
The request for the image JSON returns this:
{'collection': {'href': 'https://images-api.nasa.gov/asset/200907190008HQ', 'items': [{'href': 'http://images-assets.nasa.gov/image/200907190008HQ/200907190008HQ~orig.tif'}, {'href': 'http://images-assets.nasa.gov/image/200907190008HQ/200907190008HQ~large.jpg'}, {'href': 'http://images-assets.nasa.gov/image/200907190008HQ/200907190008HQ~medium.jpg'}, {'href': 'http://images-assets.nasa.gov/image/200907190008HQ/200907190008HQ~small.jpg'}, {'href': 'http://images-assets.nasa.gov/image/200907190008HQ/200907190008HQ~thumb.jpg'}, {'href': 'http://images-assets.nasa.gov/image/200907190008HQ/metadata.json'}], 'version': '1.0'}}
The last two lines in your Python code will extract the URLs from the JSON. Now you have all the pieces you need to write a basic user interface!
Designing the User Interface
There are many different ways you could design your image downloading application. You will be doing what is simplest as that is almost always the quickest way to create a prototype. The nice thing about prototyping is that you end up with all the pieces you will need to create a useful application. Then you can take your knowledge and either enhance the prototype or create something new with the knowledge you have gained.
Here’s a mockup of what you will be attempting to create:
As you can see, you will want an application with the following features:
- A search bar
- A widget to hold the search results
- A way to display an image when a result is chosen
- The ability to download the image
Let’s learn how to create this user interface now!
Creating the NASA Search Application
Rapid prototyping is an idea in which you will create a small, runnable application as quickly as you can. Rather than spending a lot of time getting all the widgets laid out, let’s add them from top to bottom in the application. This will give you something to work with more quickly than creating a series of nested sizers will.
Let’s start by creating a script called nasa_search_ui.py:
# nasa_search_ui.py import os import requests import wx from download_dialog import DownloadDialog from ObjectListView import ObjectListView, ColumnDefn from urllib.parse import urlencode, quote_plus
Here you import a few new items that you haven’t seen as of yet. The first is the requests package. This is a handy package for downloading files and doing things on the Internet with Python. Many developers feel that it is better than Python’s own urllib. You will need to install it to use it though. You will also need to instal ObjectListView.
Here is how you can do that with pip:
pip install requests ObjectListView
The other piece that is new are the imports from urllib.parse. You will be using this module for encoding URL parameters. Lastly, the DownloadDialog is a class for a small dialog that you will be creating for downloading NASA images.
Since you will be using ObjectListView in this application, you will need a class to represent the objects in that widget:
class Result: def __init__(self, item): data = item['data'][0] self.title = data['title'] self.location = data.get('location', '') self.nasa_id = data['nasa_id'] self.description = data['description'] self.photographer = data.get('photographer', '') self.date_created = data['date_created'] self.item = item if item.get('links'): try: self.thumbnail = item['links'][0]['href'] except: self.thumbnail = ''
The Result class is what you will be using to hold that data that makes up each row in your ObjectListView. The item parameter is a portion of JSON that you are receiving from NASA as a response to your query. In this class, you will need to parse out the information you require.
In this case, you want the following fields:
- Title
- Location of image
- NASA’s internal ID
- Description of the photo
- The photographer’s name
- The date the image was created
- The thumbnail URL
Some of these items aren’t always included in the JSON response, so you will use the dictionary’s get() method to return an empty string in those cases.
Now let’s start working on the UI:
class MainPanel(wx.Panel): def __init__(self, parent): super().__init__(parent) self.search_results = [] self.max_size = 300 self.paths = wx.StandardPaths.Get() font = wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL) main_sizer = wx.BoxSizer(wx.VERTICAL)
The MainPanel is where the bulk of your code will be. Here you do some housekeeping and create a search_results to hold a list of Result objects when the user does a search. You also set the max_size of the thumbnail image, the font to be used, the sizer and you get some StandardPaths as well.
Now let’s add the following code to the __init__():
txt = 'Search for images on NASA' label = wx.StaticText(self, label=txt) main_sizer.Add(label, 0, wx.ALL, 5) self.search = wx.SearchCtrl( self, style=wx.TE_PROCESS_ENTER, size=(-1, 25)) self.search.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN, self.on_search) self.search.Bind(wx.EVT_TEXT_ENTER, self.on_search) main_sizer.Add(self.search, 0, wx.EXPAND)
Here you create a header label for the application using wx.StaticText. Then you add a wx.SearchCtrl, which is very similar to a wx.TextCtrl except that it has special buttons built into it. You also bind the search button’s click event (EVT_SEARCHCTRL_SEARCH_BTN) and EVT_TEXT_ENTER to a search related event handler (on_search).
The next few lines add the search results widget:
self.search_results_olv = ObjectListView( self, style=wx.LC_REPORT | wx.SUNKEN_BORDER) self.search_results_olv.SetEmptyListMsg("No Results Found") self.search_results_olv.Bind(wx.EVT_LIST_ITEM_SELECTED, self.on_selection) main_sizer.Add(self.search_results_olv, 1, wx.EXPAND) self.update_search_results()
This code sets up the ObjectListView in much the same way as some of my other articles use it. You customize the empty message by calling SetEmptyListMsg() and you also bind the widget to EVT_LIST_ITEM_SELECTED so that you do something when the user selects a search result.
Now let’s add the rest of the code to the __init__() method:
main_sizer.AddSpacer(30) self.title = wx.TextCtrl(self, style=wx.TE_READONLY) self.title.SetFont(font) main_sizer.Add(self.title, 0, wx.ALL|wx.EXPAND, 5) img = wx.Image(240, 240) self.image_ctrl = wx.StaticBitmap(self, bitmap=wx.Bitmap(img)) main_sizer.Add(self.image_ctrl, 0, wx.CENTER|wx.ALL, 5 ) download_btn = wx.Button(self, label='Download Image') download_btn.Bind(wx.EVT_BUTTON, self.on_download) main_sizer.Add(download_btn, 0, wx.ALL|wx.CENTER, 5) self.SetSizer(main_sizer)
These final few lines of code add a title text control and an image widget that will update when a result is selected. You also add a download button to allow the user to select which image size they would like to download. NASA usually gives several different versions of the image from thumbnail all the way up to the original TIFF image.
The first event handler to look at is on_download():
def on_download(self, event): selection = self.search_results_olv.GetSelectedObject() if selection: with DownloadDialog(selection) as dlg: dlg.ShowModal()
Here you call GetSelectedObject() to get the user’s selection. If the user hasn’t selected anything, then this method exits. On the other hand, if the user has selected an item, then you instantiate the DownloadDialog and show it to the user to allow them to download something.
Now let’s learn how to do a search:
def on_search(self, event): search_term = event.GetString() if search_term: query = {'q': search_term, 'media_type': 'image'} full_url = base_url + '?' + urlencode(query, quote_via=quote_plus) r = requests.get(full_url) data = r.json() self.search_results = [] for item in data['collection']['items']: if item.get('data') and len(item.get('data')) > 0: data = item['data'][0] if data['title'].strip() == '': # Skip results with blank titles continue result = Result(item) self.search_results.append(result) self.update_search_results()
The on_search() event handler will get the string that the user has entered into the search control or return an empty string. Assuming that the user actually enters something to search for, you use NASA’s general search query, q and hard code the media_type to image. Then you encode the query into a properly formatted URL and use requests.get() to request a JSON response.
Next you attempt to loop over the results of the search. Note that is no data is returned, this code will fail and cause an exception to be thrown. But if you do get data, then you will need to parse it to get the bits and pieces you need.
You will skip items that don’t have the title field set. Otherwise you will create a Result object and add it to the search_results list. At the end of the method, you tell your UI to update the search results.
Before we get to that function, you will need to create on_selection():
def on_selection(self, event): selection = self.search_results_olv.GetSelectedObject() self.title.SetValue(f'{selection.title}') if selection.thumbnail: self.update_image(selection.thumbnail) else: img = wx.Image(240, 240) self.image_ctrl.SetBitmap(wx.Bitmap(img)) self.Refresh() self.Layout()
Once again, you get the selected item, but this time you take that selection and update the title text control with the selection’s title text. Then you check to see if there is a thumbnail and update that accordingly if there is one. When there is no thumbnail, you set it back to an empty image as you do not want it to keep showing a previously selected image.
The next method to create is update_image():
def update_image(self, url): filename = url.split('/')[-1] tmp_location = os.path.join(self.paths.GetTempDir(), filename) r = requests.get(url) with open(tmp_location, "wb") as thumbnail: thumbnail.write(r.content) if os.path.exists(tmp_location): img = wx.Image(tmp_location, wx.BITMAP_TYPE_ANY) W = img.GetWidth() H = img.GetHeight() if W > H: NewW = self.max_size NewH = self.max_size * H / W else: NewH = self.max_size NewW = self.max_size * W / H img = img.Scale(NewW,NewH) else: img = wx.Image(240, 240) self.image_ctrl.SetBitmap(wx.Bitmap(img)) self.Refresh() self.Layout()
The update_image() accepts a URL as its sole argument. It takes this URL and splits off the filename. Then it creates a new download location, which is the computer’s temp directory. Your code then downloads the image and checks to be sure the file saved correctly. If it did, then the thumbnail is loaded using the max_size that you set; otherwise you set it to use a blank image.
The last couple of lines Refresh() and Layout() the panel so that the widget appear correctly.
Finally you need to create the last method:
def update_search_results(self): self.search_results_olv.SetColumns([ ColumnDefn("Title", "left", 250, "title"), ColumnDefn("Description", "left", 350, "description"), ColumnDefn("Photographer", "left", 100, "photographer"), ColumnDefn("Date Created", "left", 150, "date_created") ]) self.search_results_olv.SetObjects(self.search_results)
Here you create the frame, set the title and initial size and add the panel. Then you show the frame.
This is what the main UI will look like:
Now let’s learn what goes into making a download dialog!
The Download Dialog
The download dialog will allow the user to download one or more of the images that they have selected. There are almost always at least two versions of every image and sometimes five or six.
The first piece of code to learn about is the first few lines:
# download_dialog.py import requests import wx wildcard = "All files (*.*)|*.*"
Here you once again import requests and set up a wildcard that you will use when saving the images.
Now let’s create the dialog’s __init__():
class DownloadDialog(wx.Dialog): def __init__(self, selection): super().__init__(None, title='Download images') self.paths = wx.StandardPaths.Get() main_sizer = wx.BoxSizer(wx.VERTICAL) self.list_box = wx.ListBox(self, choices=[], size=wx.DefaultSize) urls = self.get_image_urls(selection) if urls: choices = {url.split('/')[-1]: url for url in urls if 'jpg' in url} for choice in choices: self.list_box.Append(choice, choices[choice]) main_sizer.Add(self.list_box, 1, wx.EXPAND|wx.ALL, 5) save_btn = wx.Button(self, label='Save') save_btn.Bind(wx.EVT_BUTTON, self.on_save) main_sizer.Add(save_btn, 0, wx.ALL|wx.CENTER, 5) self.SetSizer(main_sizer)
In this example, you create a new reference to StandardPaths and add a wx.ListBox. The list box will hold the variants of the photos that you can download. It will also automatically add a scrollbar should there be too many results to fit on-screen at once. You call get_image_urls with the passed in selection object to get a list of urls. Then you loop over the urls and extract the ones that have jpg in their name. This does result in you missing out on alternate image files types, such as PNG or TIFF.
This gives you an opportunity to enhance this code and improve it. The reason that you are filtering the URLs is that the results usually have non-image URLs in the mix and you probably don’t want to show those as potentially downloadable as that would be confusing to the user.
The last widget to be added is the “Save” button. You could add a “Cancel” button as well, but the dialog has an exit button along the top that works, so it’s not required.
Now it’s time to learn what get_image_urls() does:
def get_image_urls(self, item): asset_url = f'https://images-api.nasa.gov/asset/{item.nasa_id}' image_request = requests.get(asset_url) image_json = image_request.json() try: image_urls = [url['href'] for url in image_json['collection']['items']] except: image_urls = [] return image_urls
This event handler is activated when the user presses the “Save” button. When the user tries to save something without selecting an item in the list box, it will return -1. Should that happen, you show them a MessageDialog to tell them that they might want to select something. When they do select something, you will show them a wx.FileDialog that allows them to choose where to save the file and what to call it.
The event handler calls the save() method, so that is your next project:
def save(self, path): selection = self.list_box.GetSelection() r = requests.get( self.list_box.GetClientData(selection)) try: with open(path, "wb") as image: image.write(r.content) message = 'File saved successfully' with wx.MessageDialog(None, message=message, caption='Save Successful', style=wx.ICON_INFORMATION) as dlg: dlg.ShowModal() except: message = 'File failed to save!' with wx.MessageDialog(None, message=message, caption='Save Failed', style=wx.ICON_ERROR) as dlg: dlg.ShowModal()
Here you get the selection again and use the requests package to download the image. Note that there is no check to make sure that the user has added an extension, let along the right extension. You can add that yourself when you get a chance.
Anyway, when the file is finished downloading, you will show the user a message letting them know.
If an exception occurs, you can show them a dialog that let’s them know that too!
Here is what the download dialog looks like:
Now let’s add some new functionality!
Adding Advanced Search
There are several fields that you can use to help narrow your search. However you don’t want to clutter your user interface with them unless the user really wants to use those filters. To allow for that, you can add an “Advanced Search” option.
Adding this option requires you to rearrange your code a bit, so let’s copy your nasa_search_ui.py file and your download_dialog.py module to a new folder called version_2.
Now rename nasa_search_ui.py to main.py to make it more obvious which script is the main entry point for your program. To make things more modular, you will be extracting your search results into its own class and have the advanced search in a separate class. This means that you will have three panels in the end:
- The main panel
- The search results panel
- The advanced search panel
Here is what the main dialog will look like when you are finished:
Let’s go over each of these separately.
The main.py Script
The main module is your primary entry point for your application. An entry point is the code that your user will run to launch your application. It is also the script that you would use if you were to bundle up your application into an executable.
Let’s take a look at how your main module starts out:
# main.py import wx from advanced_search import RegularSearch from regular_search import SearchResults from pubsub import pub class MainPanel(wx.Panel): def __init__(self, parent): super().__init__(parent) pub.subscribe(self.update_ui, 'update_ui') self.main_sizer = wx.BoxSizer(wx.VERTICAL) search_sizer = wx.BoxSizer()
This example imports both of your search-related panels:
- AdvancedSearch
- RegularSearch
It also uses pubsub to subscribe to an update topic.
Let’s find out what else is in the __init__():
txt = 'Search for images on NASA' label = wx.StaticText(self, label=txt) self.main_sizer.Add(label, 0, wx.ALL, 5) self.search = wx.SearchCtrl( self, style=wx.TE_PROCESS_ENTER, size=(-1, 25)) self.search.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN, self.on_search) self.search.Bind(wx.EVT_TEXT_ENTER, self.on_search) search_sizer.Add(self.search, 1, wx.EXPAND) self.advanced_search_btn = wx.Button(self, label='Advanced Search', size=(-1, 25)) self.advanced_search_btn.Bind(wx.EVT_BUTTON, self.on_advanced_search) search_sizer.Add(self.advanced_search_btn, 0, wx.ALL, 5) self.main_sizer.Add(search_sizer, 0, wx.EXPAND)
Here you add the title for the page along with the search control widget as you did before. You also add the new Advanced Search button and use a new sizer to contain the search widget and the button. You then add that sizer to your main sizer.
Now let’s add the panels:
self.search_panel = RegularSearch(self) self.advanced_search_panel = AdvancedSearch(self) self.advanced_search_panel.Hide() self.main_sizer.Add(self.search_panel, 1, wx.EXPAND) self.main_sizer.Add(self.advanced_search_panel, 1, wx.EXPAND)
In this example, you instantiate the RegularSearch and the AdvancedSearch panels. Since the RegularSearch is the default, you hide the AdvancedSearch from the user on startup.
Now let’s update on_search():
def on_search(self, event): search_results = [] search_term = event.GetString() if search_term: query = {'q': search_term, 'media_type': 'image'} pub.sendMessage('search_results', query=query)
The on_search() method will get called when the user presses “Enter / Return” on their keyboard or when they press the search button icon in the search control widget. If the user has entered a search string into the search control, a search query will be constructed and then sent off using pubsub.
Let’s find out what happens when the user presses the Advanced Search button:
def on_advanced_search(self, event): self.search.Hide() self.search_panel.Hide() self.advanced_search_btn.Hide() self.advanced_search_panel.Show() self.main_sizer.Layout()
When on_advanced_search() fires, it hides the search widget, the regular search panel and the advanced search button. Next, it shows the advanced search panel and calls Layout() on the main_sizer. This will cause the panels to switch out and resize to fit properly within the frame.
The last method to create is update_ui():
def update_ui(self): """ Hide advanced search and re-show original screen Called by pubsub when advanced search is invoked """ self.advanced_search_panel.Hide() self.search.Show() self.search_panel.Show() self.advanced_search_btn.Show() self.main_sizer.Layout()
The update_ui() method is called when the user does an Advanced Search. This method is invoked by pubsub. It will do the reverse of on_advanced_search() and un-hide all the widgets that were hidden when the advanced search panel was shown. It will also hide the advanced search panel.
The frame code is the same as it was before, so it is not shown here.
Let’s move on and learn how the regular search panel is created!
The regular_search.py Script
The regular_search module is your refactored module that contains the ObjectListView that will show your search results. It also has the Download button on it.
The following methods / classes will not be covered as they are the same as in the previous iteration:
- on_download()
- on_selection()
- update_image()
- update_search_results()
- The Result class
Let’s get started by seeing how the first few lines in the module are laid out:
# regular_search.py import os import requests import wx from download_dialog import DownloadDialog from ObjectListView import ObjectListView, ColumnDefn from pubsub import pub from urllib.parse import urlencode, quote_plus base_url = 'https://images-api.nasa.gov/search'
Here you have all the imports you had in the original nasa_search_ui.py script from version_1. You also have the base_url that you need to make requests to NASA’s image API. The only new import is for pubsub.
Let’s go ahead and create the RegularSearch class:
class RegularSearch(wx.Panel): def __init__(self, parent): super().__init__(parent) self.search_results = [] self.max_size = 300 font = wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL) main_sizer = wx.BoxSizer(wx.VERTICAL) self.paths = wx.StandardPaths.Get() pub.subscribe(self.load_search_results, 'search_results') self.search_results_olv = ObjectListView( self, style=wx.LC_REPORT | wx.SUNKEN_BORDER) self.search_results_olv.SetEmptyListMsg("No Results Found") self.search_results_olv.Bind(wx.EVT_LIST_ITEM_SELECTED, self.on_selection) main_sizer.Add(self.search_results_olv, 1, wx.EXPAND) self.update_search_results()
This code will initialize the search_results list to an empty list and set the max_size of the image. It also sets up a sizer and the ObjectListView widget that you use for displaying the search results to the user. The code is actually quite similar to the first iteration of the code when all the classes were combined.
Here is the rest of the code for the __init__():
main_sizer.AddSpacer(30) self.title = wx.TextCtrl(self, style=wx.TE_READONLY) self.title.SetFont(font) main_sizer.Add(self.title, 0, wx.ALL|wx.EXPAND, 5) img = wx.Image(240, 240) self.image_ctrl = wx.StaticBitmap(self, bitmap=wx.Bitmap(img)) main_sizer.Add(self.image_ctrl, 0, wx.CENTER|wx.ALL, 5 ) download_btn = wx.Button(self, label='Download Image') download_btn.Bind(wx.EVT_BUTTON, self.on_download) main_sizer.Add(download_btn, 0, wx.ALL|wx.CENTER, 5) self.SetSizer(main_sizer)
The first item here is to add a spacer to the main_sizer. Then you add the title and the img related widgets. The last widget to be added is still the download button.
Next, you will need to write a new method:
def reset_image(self): img = wx.Image(240, 240) self.image_ctrl.SetBitmap(wx.Bitmap(img)) self.Refresh()
The reset_image() method is for resetting the wx.StaticBitmap back to an empty image. This can happen when the user uses the regular search first, selects an item and then decides to do an advanced search. Resetting the image prevents the user from seeing a previously selected item and potentially confusing the user.
The last method you need to add is load_search_results():
def load_search_results(self, query): full_url = base_url + '?' + urlencode(query, quote_via=quote_plus) r = requests.get(full_url) data = r.json() self.search_results = [] for item in data['collection']['items']: if item.get('data') and len(item.get('data')) > 0: data = item['data'][0] if data['title'].strip() == '': # Skip results with blank titles continue result = Result(item) self.search_results.append(result) self.update_search_results() self.reset_image()
The load_search_results() method is called using pubsub. Both the main and the advanced_search modules call it by passing in a query dictionary. Then you encode that dictionary into a formatted URL. Next you use requests to send a JSON request and you then extract the results. This is also where you call reset_image() so that when a new set of results loads, there is no result selected.
Now you are ready to create an advanced search!
The advanced_search.py Script
The advanced_search module is a wx.Panel that has all the widgets you need to do an advanced search against NASA’s API. If you read their documentation, you will find that there are around a dozen filters that can be applied to a search.
Let’s start at the top:
class AdvancedSearch(wx.Panel): def __init__(self, parent): super().__init__(parent) self.main_sizer = wx.BoxSizer(wx.VERTICAL) self.free_text = wx.TextCtrl(self) self.ui_helper('Free text search:', self.free_text) self.nasa_center = wx.TextCtrl(self) self.ui_helper('NASA Center:', self.nasa_center) self.description = wx.TextCtrl(self) self.ui_helper('Description:', self.description) self.description_508 = wx.TextCtrl(self) self.ui_helper('Description 508:', self.description_508) self.keywords = wx.TextCtrl(self) self.ui_helper('Keywords (separate with commas):', self.keywords)
The code to set up the various filters is all pretty similar. You create a text control for the filter, then you pass it into ui_helper() along with a string that is a label for the text control widget. Repeat until you have all the filters in place.
Here are the rest of the filters:
self.location = wx.TextCtrl(self) self.ui_helper('Location:', self.location) self.nasa_id = wx.TextCtrl(self) self.ui_helper('NASA ID:', self.nasa_id) self.photographer = wx.TextCtrl(self) self.ui_helper('Photographer:', self.photographer) self.secondary_creator = wx.TextCtrl(self) self.ui_helper('Secondary photographer:', self.secondary_creator) self.title = wx.TextCtrl(self) self.ui_helper('Title:', self.title) search = wx.Button(self, label='Search') search.Bind(wx.EVT_BUTTON, self.on_search) self.main_sizer.Add(search, 0, wx.ALL | wx.CENTER, 5) self.SetSizer(self.main_sizer)
At the end, you set the sizer to the main_sizer. Note that not all the filters that are in NASA’s API are implemented in this code. For example, I didn’t add media_type because this application will be hard-coded to only look for images. However if you wanted audio or video, you could update this application for that. I also didn’t include the year_start and year_end filters. Feel free to add those if you wish.
Now let’s move on and create the ui_helper() method:
def ui_helper(self, label, textctrl): sizer = wx.BoxSizer() lbl = wx.StaticText(self, label=label, size=(150, -1)) sizer.Add(lbl, 0, wx.ALL, 5) sizer.Add(textctrl, 1, wx.ALL | wx.EXPAND, 5) self.main_sizer.Add(sizer, 0, wx.EXPAND)
The ui_helper() takes in label text and the text control widget. It then creates a wx.BoxSizer and a wx.StaticText. The wx.StaticText is added to the sizer, as is the passed-in text control widget. Finally the new sizer is added to the main_sizer and then you’re done. This is a nice way to reduce repeated code.
The last item to create in this class is on_search():
def on_search(self, event): query = {'q': self.free_text.GetValue(), 'media_type': 'image', 'center': self.nasa_center.GetValue(), 'description': self.description.GetValue(), 'description_508': self.description_508.GetValue(), 'keywords': self.keywords.GetValue(), 'location': self.location.GetValue(), 'nasa_id': self.nasa_id.GetValue(), 'photographer': self.photographer.GetValue(), 'secondary_creator': self.secondary_creator.GetValue(), 'title': self.title.GetValue()} pub.sendMessage('update_ui') pub.sendMessage('search_results', query=query)
When the user presses the Search button, this event handler gets called. It creates the search query based on what the user has entered into each of the fields. Then the handler will send out two messages using pubsub. The first message will update the UI so that the advanced search is hidden and the search results are shown. The second message will actually execute the search against NASA’s API.
Here is what the advanced search page looks like:
Now let’s update the download dialog.
The download_dialog.py Script
The download dialog has a couple of minimal changes to it. Basically you need to add an import of Python’s os module and then update the save() function.
Add the following lines to the beginning of the function:
def save(self, path): _, ext = os.path.splitext(path) if ext.lower() != '.jpg': path = f'{path}.jpg'
This code was added to account for the case where the user does not specify the extension of the image in the saved file name.
Wrapping Up
This article covered a lot of fun new information. You learned one approach for working with an open API that doesn’t have a Python wrapper already around it. You discovered the importance of reading the API documentation and then added a user interface to that API. Then you learned how to parse JSON and download images from the Internet.
While it is not covered here, Python has a json module that you could use as well.
Here are some ideas for enhancing this application:
- Caching search results
- Downloading thumbnails in the background
- Downloading links in the background
You could use threads to download the thumbnails and the larger images as well as for doing the web requests in general. This would improve the performance of your application. You may have noticed that the application became slightly unresponsive, depending on your Internet connectivity. This is because when it is doing a web request or downloading a file, it blocks the UI’s main loop. You should give threads a try if you find that sort of thing bothersome.
Download the Code
- The source code as a tarball
Related Reading
- Creating a Calculator with wxPython
- How to Distribute a wxPython Application
Pingback: Links 19/4/2019: PyPy 7.1.1, LabPlot 2.6, Kipi Plugins 5.9.1 Released | Techrights
Pingback: ICYMI: Happy 6th Birthday MicroPython! Python for wearables, and need a lift? #Python #Adafruit #CircuitPython #PyCon2019 @circuitpython @micropython @ThePSF @Adafruit « Adafruit Industries – Makers, hackers, artists, designers and engineers!
Pingback: Creating a File Search GUI with wxPython - Mouse Vs Python