Last active
August 29, 2015 14:09
-
-
Save nealrs/4b55f81e68cf8d890db4 to your computer and use it in GitHub Desktop.
Toga demo for issue 62 of Git@Me
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Giphy API search written with Toga -- written for Issue 62 of Git@Me | |
# This is entirely cribbed from the Toga Browser tutorial. This isn't a particuarly good demo, but it'll do. @nealrs 2014 | |
#!/usr/bin/env python | |
from __future__ import print_function, unicode_literals, absolute_import | |
import urllib | |
import json | |
import toga | |
class giphy_search(toga.App): | |
def startup(self): | |
container = toga.Container() | |
self.webview = toga.WebView() | |
self.webview.url = "http://blerg.me" | |
self.url_input = toga.TextInput('cats') | |
go_button = toga.Button('search giphy', on_press=self.search) | |
container.add(self.webview) | |
container.add(self.url_input) | |
container.add(go_button) | |
container.constrain(self.url_input.TOP == container.TOP + 10) | |
container.constrain(self.url_input.LEFT == container.LEFT + 5) | |
container.constrain(self.url_input.RIGHT + 5 == go_button.LEFT) | |
container.constrain(go_button.TOP == container.TOP + 10) | |
container.constrain(go_button.RIGHT + 5 == container.RIGHT) | |
container.constrain(self.webview.TOP == self.url_input.BOTTOM + 10) | |
container.constrain(self.webview.BOTTOM == container.BOTTOM) | |
container.constrain(self.webview.RIGHT == container.RIGHT) | |
container.constrain(self.webview.LEFT == container.LEFT) | |
app.main_window.content = container | |
def search(self, widget): | |
s = (self.url_input.value).replace(" ", "+") | |
d = json.loads(urllib.urlopen("http://api.giphy.com/v1/gifs/search?q="+s+"&api_key=dc6zaTOxFJmzC&limit=1").read()) | |
q = d['data'][0]['embed_url'] | |
self.webview.url = q | |
if __name__ == '__main__': | |
app = giphy_search('Giphy Search', 'me.gitat.giphy_Search') | |
app.main_loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment