def create_file(team_id=None, channel=None, params=None): event = Misc.array_pop(params) # original slack event object if not params or len(params) < 2: return send_message( ':red_circle: You must provide the following params: `Server Id` and `notebook path`', channel, team_id) build_id = str(Misc.array_pop(params, 0)) file_path = Misc.array_pop(params, 0) file_contents = ' '.join(params) notebook = Live_Notebook() file_type = 'notebook' if '.ipynb' in file_path else 'file' if notebook.set_build_from_short_id(build_id) is None: return ':red_circle: Could not find Jupyter server with id `{0}`. Please use `jupyter servers` to see the current list of live servers'.format( build_id) send_message( ':point_right: Creating `{0}` on server `{1}` at location `{2}` with content of size `{3}`' .format(file_type, build_id, file_path, len(file_contents)), channel, team_id) jupyter_api = notebook.jupyter_api() if file_type == 'notebook': result = jupyter_api.notebook_create(file_path, file_contents) else: result = jupyter_api.file_create(file_path, file_contents) if result.get('status') == 'ok': if file_type == 'notebook': url = "{0}/notebooks/{1}".format(jupyter_api.server, file_path) else: url = "{0}/edit/{1}".format(jupyter_api.server, file_path) return send_message( ':white_check_mark: `{0}` created ok, you can see it here: {1}' .format(file_type, url), channel, team_id) else: return send_message( ':red_circle: Error creating notebook ```{0}```'.format( result.get('data')), channel, team_id)
class test_Live_Notebook(TestCase): def setUp(self): self.short_id = '12d62' self.notebook = Live_Notebook(short_id=self.short_id, headless=True) self.test_notebook ='notebooks/users/gsbot/gsbot-invoke.ipynb' self.result = None self.png_data = None def tearDown(self): if self.result is not None: Dev.pprint(self.result) if self.png_data: png_file = '/tmp/lambda_png_file.png' with open(png_file, "wb") as fh: fh.write(base64.decodebytes(self.png_data.encode())) Dev.pprint("Png data with size {0} saved to {1}".format(len(self.png_data), png_file)) # config methods def test_set_build_from_short_id(self): assert self.notebook.set_build_from_short_id(self.short_id) is self.notebook assert self.notebook.set_build_from_short_id('aaaa' ) is None assert self.notebook.set_build_from_short_id('gscs' ) is self.notebook # will need this server to be running assert self.notebook.set_build_from_short_id('aaaa' ) is None def test_jupyter_api(self): assert self.notebook.jupyter_api().version() == {'version': '5.7.8'} assert set(self.notebook.jupyter_api().status()) == {'connections', 'kernels', 'last_activity', 'started'} def test_stop(self): self.result = self.notebook.set_build_from_short_id(self.short_id).stop() # api methods def test_files(self): self.result = self.notebook.files('users') #contents def test_execute_command(self): jp_web = self.notebook.jupyter_web() jp_cell = self.notebook.jupyter_cell() #jp_web.login() if (self.test_notebook in jp_web.url()) is False: jp_web.open(self.test_notebook) jp_cell.clear() jp_cell.execute(""" answer=40+2 print('this was executed from an unit test') answer """) self.result = jp_cell.output_wait_for_data() def test_execute_python(self): self.result = self.notebook.execute_python(""" a=40+2 print(123) a """, keep_contents=None) def test_screenshot(self): self.notebook.set_build_from_short_id(self.short_id) self.png_data = self.notebook.screenshot(self.test_notebook,800) def test_bug_screenshot_duplicates_first_page(self): target_notebook = 'nbconvert/html/users/dinis/rdf/part-1-loading-the-rdf-file.ipynb?download=false' #jp_web = self.notebook.jupyter_web() #jp_web.open(self.notebook) self.png_data = self.notebook.screenshot(target_notebook, 2000,10000)
def update_notebook(team_id=None, channel=None, params=None): event = Misc.array_pop(params) # original slack event object if not params or len(params) < 2: return send_message( ':red_circle: You must provide the following params: `Server Id` and `notebook path` (to update)', channel, team_id) short_id = str(Misc.array_pop(params, 0)) target_notebook = Misc.array_pop(params, 0) if '.ipynb' not in target_notebook: target_notebook += '.ipynb' notebook = Live_Notebook() if notebook.set_build_from_short_id(short_id) is None: return send_message( ':red_circle: Could not find Jupyter server with id `{0}`. Please use `jupyter servers` to see the current list of live servers' .format(short_id), channel, team_id) if notebook.jupyter_api().contents(target_notebook) is None: return send_message( ":red_circle: Could not find notebook `{0}` in server `{1}`". format(target_notebook, short_id), channel, team_id) send_message( ":point_right: Updating notebook `{0}` on server `{1}`".format( target_notebook, short_id), channel, team_id) target_notebook_fixed = "notebooks/{0}".format(target_notebook) code = '!cd ../../..; jupyter nbconvert --to notebook --inplace --execute {0}'.format( target_notebook_fixed) #note for longer executions the save is not working ok (invoke_notebook, created) = notebook.get_python_invoke_file() send_message( ':point_right: Running code with size `{0}` on server `{1}` (on file `{2}`)' .format(len(code), short_id, invoke_notebook), channel, team_id) result = notebook.execute_python_in_notebook(invoke_notebook, code, event) #send_message("result: ```{0}``` ".format(result),channel, team_id) if result and ('[NbConvertApp] Writing' not in result): #or ('[js eval error]' in result): if 'matched no files' in result: send_message( ":red_circle: Update failed, could not find notebook \n ```{0}```" .format(target_notebook_fixed), channel, team_id) send_message( "Here is the execution code: ```{0}````".format(code), channel, team_id) return else: return send_message( ":red_circle: Update failed: \n ```{0}```".format(result), channel, team_id) send_message(":point_right: Notebook updated ok", channel, team_id) # need to double check params = [short_id, target_notebook, event] return Jupyter_Web_Commands.preview(team_id, channel, params)