-
Notifications
You must be signed in to change notification settings - Fork 0
/
filedialog.py
48 lines (39 loc) · 1.43 KB
/
filedialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Modified from the work of James Draper
# https://codereview.stackexchange.com/questions/162920/file-selection-button-for-jupyter-notebook
# This file is licensed under the Creative Commons Attribution-ShareAlike
# license (CC BY-SA 3.0)
# https://creativecommons.org/licenses/by-sa/3.0/
import traitlets
from ipywidgets import Button, Box, Label
from IPython.display import display
from tkinter import Tk, filedialog
class SaveButton(Button):
save_function = None
def __init__(self):
super(SaveButton, self).__init__()
# Add the selected_files trait
self.add_traits(path=traitlets.traitlets.Unicode())
# Create the button.
self.description="Save graph"
self.icon="save"
# Set on click behavior.
self.on_click(self.save_file)
def select_file(self):
"""Generate instance of tkinter.filedialog.
Parameters
----------
button : obj:
An instance of ipywidgets.widgets.Button
"""
# Create Tk root
root = Tk()
# Hide the main window
root.withdraw()
# Raise the root to the top of all windows.
root.call('wm', 'attributes', '.', '-topmost', True)
# Path to use to save the file
self.path = filedialog.asksaveasfilename()
def save_file(self, button=None):
if self.save_function is not None:
self.select_file()
self.save_function(self.path)