def open_file_dialog(self): file_types = ('Text Files (*.txt;*.sh;*.md;*.py;*.c)', 'All files (*.*)') file_name = self.window.create_file_dialog(webview.OPEN_DIALOG, allow_multiple=False, file_types=file_types) if file_name and file_name[0]: with open(file_name[0]) as f: return Viaduc.callback('loadFile', {'fileName': file_name, 'content': f.read()}) else: return Viaduc.done()
def do_something(self, vals): print('do_something: {}'.format(vals)) v = self.map_vals(vals) if not v['inputEmail4']: raise ValueError('Empty email') if not v['inputPassword4']: raise ValueError('Empty password') if v['inputCheck']: return Viaduc.callback('callback', {'message': 'This is a sample callback'}) else: return Viaduc.done('Operation completed')
def open_dir_dialog(self, dir_name): if not dir_name: dir_name = self.get_dir_name() if not dir_name: # cancel was pressed return Viaduc.done() image_files = [] for f in os.listdir(dir_name): if f.endswith('.png') or f.endswith('.jpg') or f.endswith('.gif'): image_files.append(f) if not image_files: raise ValueError(f'No image files in {dir_name}') return Viaduc.callback('loadDir', {'dir': dir_name, 'imageFiles': image_files})
def do_something(self, vals): v = self.map_vals(vals) if not v['_email']: raise ValueError('Empty email') if not v['_password']: raise ValueError('Empty password') print(v) return Viaduc.done('Operation completed')
def save_file_dialog(self, vals): v = self.map_vals(vals) file_name = self.window.create_file_dialog(webview.SAVE_DIALOG, directory=os.path.dirname(v['_fileName']), save_filename=os.path.basename(v['_fileName'])) if file_name: with open(file_name, "w+") as f: f.write(v['_editor']) return Viaduc.done()
def convert(self, vals): v = self.map_vals(vals) if not v['_fahrenheit']: raise ValueError('Enter a temperature') return Viaduc.callback('showCelsius', {'celsius': fahrenheit_to_celsius(v['_fahrenheit'])})
#! /usr/bin/env python3 import sys from viaduc import Viaduc def fahrenheit_to_celsius(fahrenheit): return round((5 / 9) * (float(fahrenheit) - 32), 2) class Api(Viaduc.Api): def convert(self, vals): v = self.map_vals(vals) if not v['_fahrenheit']: raise ValueError('Enter a temperature') return Viaduc.callback('showCelsius', {'celsius': fahrenheit_to_celsius(v['_fahrenheit'])}) class Presentation(Viaduc.Presentation): width = 320 height = 468 title = 'temperature converter' html = ''' <!-- copy file here --> ''' file = "temperature-converter.html" if __name__ == '__main__': Viaduc(api=Api(), presentation=Presentation(), args=sys.argv + ['--frameless'])
doAlert('alert-danger', 'error', e); }); } function deviceClick(e) { pywebview.api.click(e.pageX * 4, e.pageY * 4) .then(obtainScreenshot) .catch(e => { doAlert('alert-danger', 'error', e); }); } </script> </body> </html> ''' class Presentation(Viaduc.Presentation): html = HTML title = 'android device viewer' frameless = True window_background_color = '#000' display_real_size = culebratester.get_display_real_size() width = display_real_size.x / 4 height = display_real_size.y / 4 if __name__ == '__main__': Viaduc(api=Api(), presentation=Presentation(), args=sys.argv + ['--debug'])
from viaduc import Viaduc class Api(Viaduc.Api): def do_something(self, vals): print('do_something: {}'.format(vals)) v = self.map_vals(vals) if not v['inputEmail4']: raise ValueError('Empty email') if not v['inputPassword4']: raise ValueError('Empty password') if v['inputCheck']: return Viaduc.callback('callback', {'message': 'This is a sample callback'}) else: return Viaduc.done('Operation completed') class Presentation(Viaduc.Presentation): width = 800 height = 520 title = 'complex form example' # NOTE: Usually we want html to be a string so everything is contained in one file, however in this case we are # reading it from a file as an example of how it can be done. file = "complexform-mui.html" if __name__ == '__main__': Viaduc(api=Api(), presentation=Presentation(), args=sys.argv)
<div class="jumbotron"> <h1>{{title}}</h1> <p class="lead">Welcome to <em>Viaduc</em>, the simplest way of creating a GUI in python.</p> </div> <div class="container-fluid"> <div class="row"> <div class="col"> <button type="button" class="btn btn-success" style="min-width: 100%" onclick="processResponse(Promise.resolve({action:'SUCCESS', message:'Hello'}));">Success</button> </div> <div class="col"> <button type="button" class="btn btn-danger" style="min-width: 100%" onclick="processResponse(Promise.resolve({action:'ERROR', message:'Danger'}));">Error</button> </div> <div class="col"> <button type="button" class="btn btn-warning" style="min-width: 100%" onclick="processResponse(Promise.resolve({action:'WARNING', message:'Warning'}));">Warning</button> </div> </div> </div> {{bootstrap_js}} </body> </html> ''' if __name__ == '__main__': Viaduc(presentation=Presentation(), args=['', '--debug'])
def load_image(self, image): mime, encoding = mimetypes.guess_type(image) with open(image, 'rb') as i: data = base64.b64encode(i.read()).decode("ascii") src = f'data:{mime};base64,{data}' return Viaduc.callback('showImage', {'image': image, 'src': src})
def get_vals(self, vals): v = self.map_vals(vals) return Viaduc.done(f'vals: {v}')
<div class="row mb-3"> <div class="col-12"> <label for="_cars">Select multiple options</label> <select name="cars" id="_cars" class="form-control custom-select" multiple> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option> </select> </div> </div> <button type="button" class="btn btn-primary" onclick="doGetVals()">Get vals</button> </div> <script> function doGetVals() { processResponse(pywebview.api.get_vals(getVals())); } </script> {{bootstrap_js}} </body> </html> ''' if __name__ == '__main__': Viaduc(api=Api(), presentation=Presentation(), args=['', '--debug'])
from viaduc import Viaduc class Presentation(Viaduc.Presentation): title = 'hello world' html = ''' <!DOCTYPE html> <html lang="en"> <head> {{bootstrap_meta}} {{bootstrap_css}} <title>{{title}}</title> </head> <body> <div class="jumbotron"> <h1>{{title}}</h1> <p class="lead">Welcome to <em>Viaduc</em>, the simplest way of creating a GUI in python.</p> </div> {{bootstrap_js}} </body> </html> ''' if __name__ == '__main__': Viaduc(presentation=Presentation())
#! /usr/bin/env python3 from viaduc import Viaduc if __name__ == '__main__': Viaduc()