Skip to navigation
Toga windows and linux desktop app example to download and save images
15.01.19
linux req: cairocffi==0.9.0 certifi==2018.11.29 cffi==1.11.5 chardet==3.0.4 conda==4.3.16 gbulb==0.6.1 idna==2.8 pycosat==0.6.3 pycparser==2.19 PyGObject==3.31.2.dev0 requests==2.21.0 ruamel.yaml==0.15.85 toga==0.3.0.dev11 toga-core==0.3.0.dev11 toga-gtk==0.3.0.dev11 travertino==0.1.2 urllib3==1.24.1 windows req: arrow==0.13.0 binaryornot==0.4.4 boto3==1.9.79 botocore==1.12.79 briefcase==0.2.8 certifi==2018.11.29 chardet==3.0.4 Click==7.0 cookiecutter==1.6.0 docutils==0.14 future==0.17.1 idna==2.8 Jinja2==2.10 jinja2-time==0.2.0 jmespath==0.9.3 MarkupSafe==1.1.0 poyo==0.4.2 python-dateutil==2.7.5 pythonnet==2.3.0 requests==2.21.0 s3transfer==0.1.13 six==1.12.0 toga==0.3.0.dev11 toga-core==0.3.0.dev11 toga-winforms==0.3.0.dev11 travertino==0.1.2 urllib3==1.23 voc==0.1.6 whichcraft==0.5.2 setup.py file: e: raise RuntimeError("Unable to find version string.") with io.open('README.rst', encoding='utf8') as readme: long_description = readme.read() setup( name='picture_downloader', version=version, description='Download Pictures from Foo bar', long_description=long_description, author='foo', author_email='foo@bar.com', license='GNU General Public License v3 or later (GPLv3+)', packages=find_packages( exclude=[ 'docs', 'tests', 'windows', 'macOS', 'linux', 'iOS', 'android', 'django' ] ), classifiers=[ 'Development Status :: 1 - Planning', 'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)', ], install_requires=[ ], options={ 'app': { 'formal_name': 'foo bar Picture Downloader', 'bundle': 'com.foo' }, # Desktop/laptop deployments 'macos': { 'app_requires': [ 'toga-cocoa==0.3.0.dev11', ] }, 'linux': { 'app_requires': [ 'toga-gtk==0.3.0.dev11', ] }, 'windows': { 'app_requires': [ 'arrow==0.13.0', 'binaryornot==0.4.4', 'boto3==1.9.79', 'botocore==1.12.79', 'briefcase==0.2.8', 'certifi==2018.11.29', 'chardet==3.0.4', 'Click==7.0', 'cookiecutter==1.6.0', 'docutils==0.14', 'future==0.17.1', 'idna==2.8', 'Jinja2==2.10', 'jinja2-time==0.2.0', 'jmespath==0.9.3', 'MarkupSafe==1.1.0', 'poyo==0.4.2', 'python-dateutil==2.7.5', 'pythonnet==2.3.0', 'requests==2.21.0', 's3transfer==0.1.13', 'six==1.12.0', 'toga==0.3.0.dev11', 'toga-core==0.3.0.dev11', 'toga-winforms==0.3.0.dev11', 'travertino==0.1.2', 'urllib3==1.23', 'voc==0.1.6', 'whichcraft==0.5.2' ], 'icon': 'images/windows', }, # Mobile deployments 'ios': { 'app_requires': [ 'toga-ios==0.3.0.dev11', ] }, 'android': { 'app_requires': [ 'toga-android==0.3.0.dev11', ] }, # Web deployments 'django': { 'app_requires': [ 'toga-django==0.3.0.dev11', ] }, } ) app.py file: import json import toga import requests import threading import shutil from toga.style import Pack from toga.style.pack import COLUMN, ROW, LEFT, RIGHT from pathlib import Path class Picture_Downloader(toga.App): def startup(self): self.action = False self.folder = str(Path.home()) + os.path.sep + 'Pictures' + os.path.sep + 'Foo' if not os.path.exists(self.folder): os.makedirs(self.folder) self.img_views = 'http://foo/img' # Create a main window with a name matching the app self.main_window = toga.MainWindow(title=self.name) # Create a main content box self.button = toga.Button('Download', on_press=self.download,style=Pack(flex=1,padding_left=15,padding_right=15)) styles = toga.Label('Styles:', style=Pack(text_align=LEFT,flex=1,padding_left=15,padding_right=15)) sizes = toga.Label('Sizes:', style=Pack(text_align=LEFT,flex=1,padding_left=15,padding_right=15)) logs = toga.Label('Logs:', style=Pack(text_align=LEFT,padding_left=15,padding_right=15,padding_bottom=5,padding_top=5)) self.textarea = toga.MultilineTextInput(id='view1',style=Pack(flex=1,padding_left=15,padding_right=15)) self.selector = toga.Selection(items=['120','150','200','250','350','455','456','500','600','1000','1200','1600','5000'],style=Pack(flex=1,padding_left=15,padding_right=15)) self.log = toga.MultilineTextInput(id='view1',readonly=True,style=Pack(flex=1,padding_left=15,padding_right=15)) container = toga.ScrollContainer(content=self.textarea, horizontal=False,style=Pack(flex=1,padding_left=15,padding_right=15)) container2 = toga.ScrollContainer(content=self.log, horizontal=False,style=Pack(flex=1,padding_left=15,padding_right=15)) self.progress = toga.ProgressBar() self.progress.style.update(padding_left=15,padding_right=15) #set widgets to the box box = toga.Box(style=Pack(direction=COLUMN, padding_top=10)) box_a = toga.Box(style=Pack(padding_bottom=5)) box_b = toga.Box(style=Pack(flex=1)) box_a.add(styles) box_a.add(sizes) box_b.add(container) box_b.add(self.selector) box.add(box_a) box.add(box_b) box.add(logs) box.add(container2) box.add(self.button) box.add(self.progress) self.main_window.content = box self.main_window.show() def download(self, widget): if self.action == False: self.logging('start...') styles = list(set(self.textarea.value.splitlines())) self.logging('{} styles entered'.format(len(styles))) if len(styles): self.progress.value = 0 self.action = True self.logging('trying access salamander API via post: {}'.format(self.img_views)) images = self.set_images(styles) self.ts = {} for i in images: self.progress.value += 1 self.ts[i] = threading.Thread(target=self.download_imgs, args=(i,images[i],), kwargs={}) self.ts[i].start() for i in self.ts: self.ts[i].join() self.action = False self.logging('') self.logging('') self.logging('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') self.logging('{}'.format(self.folder)) self.logging('check here:') self.logging('Completed for size: {}'.format(self.selector.value)) self.logging('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') self.logging('') self.logging('') else: self.logging('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') self.logging('Please enter styles') self.logging('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') def set_images(self,styles): imgs = {} try: headers = {'content-type': 'application/json'} data = {"keys":styles} r = requests.post(self.img_views,data=json.dumps(data),headers=headers) j = r.json() res = {} for i in j['rows']: imgs[i['key']] = i['value'] except Exception as e: self.logging('failed') self.logging(e) print(e) return False self.logging('successfully got {}x images urls'.format(len(imgs))) size = self.selector.value folders = [] images = {} for i in imgs: url = imgs[i] sep = '-{}_'.format('250') path = url.split(sep) url = url.replace(sep,'-{}_'.format(size)) img = path[1].split('_') folder = '{}{}{}{}{}'.format(self.folder,os.path.sep,size,os.path.sep,img[0]) image = '{}{}{}'.format(folder,os.path.sep,img[1]) folders.append(folder) images[i] = {'img':image,'url':url} self.progress.max = 4 folders = set(folders) for i in folders: if not os.path.exists(i): os.makedirs(i) missing = {} for i in images: if not os.path.exists(images[i]['img']): missing[i] = images[i] else: self.logging('already downloaded {}'.format(images[i]['img'])) self.progress.max = len(missing) return missing def download_imgs(self,i,img): r = requests.get(img['url'], stream=True) if r.status_code == 200: with open(img['img'], 'wb') as f: r.raw.decode_content = True shutil.copyfileobj(r.raw, f) def logging(self,msg): logs = self.log.value.splitlines() logs.insert(0, '{} ... {}'.format(datetime.datetime.now().replace(microsecond=0),msg)) self.log.value = '\n'.join(logs).strip("\n") def main(): path = os.path.dirname(os.path.abspath(__file__)) return Picture_Downloader('Foo Bar - Picture Downloader', 'com.foo.picture_downloader',os.path.join(path, "icons", "icon.png"))
https://briefcase.readthedocs.io/en/latest/tutorial/tutorial-0.html
Reply
Anonymous
Information Epoch 1732652277
Using text data files.
Home
Notebook
Contact us