### If you want to auto-save .html and .py versions of your notebook: # modified from: https://github.com/ipython/ipython/issues/8009 # Solution2: https://jupyter-notebook.readthedocs.io/en/stable/extending/savehooks.html import os from subprocess import check_call import re
defclear_prompt(dir_path, nb_fname, log_func): """remove the number in '# In[ ]:'""" name, ext = os.path.splitext(nb_fname) pattern = re.compile(r'^# In\[\d+\]:')
for n_ext in ['.py', '.txt']: script_name = os.path.join(dir_path, name+n_ext) if os.path.exists(script_name): new_lines = [] withopen(script_name, 'rt', encoding='utf-8') as f: lines = f.readlines() for line in lines: new_line = re.sub(pattern, '# In[ ]:', line) new_lines.append(new_line) withopen(script_name, 'wt', encoding='utf-8') as f: f.writelines(new_lines) log_func('Remove number in "# In[ ]:"! File Name: %s' % script_name) break
defpost_save(model, os_path, contents_manager): """post-save hook for converting notebooks to .py scripts""" if model['type'] != 'notebook': return# only do this for notebooks d, fname = os.path.split(os_path) check_call(['jupyter', 'nbconvert', '--to', 'script', fname], cwd=d) # '--no-prompt', log = contents_manager.log # log.info('Filename:%s'%fname) clear_prompt(d, fname, log.info) # check_call(['ipython', 'nbconvert', '--to', 'html', fname], cwd=d)
# pressing a key k.press_key('H') # which you then follow with a release of the key k.release_key('H') # or you can 'tap' a key which does both k.tap_key('e') # note that that tap_key does support a way of repeating keystrokes with a interval time between each k.tap_key('l',n=2,interval=5) # and you can send a string if needed too k.type_string('o World!')
#Create an Alt+Tab combo k.press_key(k.alt_key) k.tap_key(k.tab_key) k.release_key(k.alt_key)
k.tap_key(k.function_keys[5]) # Tap F5 k.tap_key(k.numpad_keys['Home']) # Tap 'Home' on the numpad k.tap_key(k.numpad_keys[5], n=3) # Tap 5 on the numpad, thrice
# Mac example k.press_keys(['Command','shift','3']) # Windows example k.press_keys([k.windows_l_key,'d'])
# Windows k.tap_key(k.alt_key) # Mac k.tap_key('Alternate')
defclick(self, x, y, button, press): '''Print Fibonacci numbers when the left click is pressed.''' if button == 1: if press: print(self.fibo.next()) else: # Exit if any other mouse button used self.stop()