Skip to content

Instantly share code, notes, and snippets.

@hkucuk
Created July 16, 2019 20:05
Show Gist options
  • Save hkucuk/7277b8a056cb8c30dba6a394bae8d096 to your computer and use it in GitHub Desktop.
Save hkucuk/7277b8a056cb8c30dba6a394bae8d096 to your computer and use it in GitHub Desktop.
PyWebView Open File Dialog Sample
import webview
import base64
def open_file_dialog(window):
file_types = ('Image Files (*.bmp;*.jpg;*.gif;*.png)',
'All files (*.*)')
path = window.create_file_dialog(webview.OPEN_DIALOG,
allow_multiple=False,
file_types=file_types)
with open(path[0], "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
window.evaluate_js(
r"""
var img = document.createElement('img');
img.src = "data:image/png;base64,%s";
document.getElementById('image-container').appendChild(img);
"""% ( encoded_string.decode('ascii'))
)
if __name__ == '__main__':
html = """
<html>
<body>
<h1>Selected Image</h1>
</body>
</html>
"""
window = webview.create_window('Open file dialog example', html=html)
webview.start(open_file_dialog, window, debug=True,gui='cef')
@sam2332
Copy link

sam2332 commented Apr 6, 2021

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment