agentscope.service.execute_code.exec_notebook module

Service for executing jupyter notebooks interactively Partially referenced the implementation of https://github.com/geekan/MetaGPT/blob/main/metagpt/actions/di/execute_nb_code.py

class agentscope.service.execute_code.exec_notebook.NoteBookExecutor(timeout: int = 300)[source]

Bases: object

Class for executing jupyter notebooks block interactively. To use the service function, you should first init the class, then call the run_code_on_notebook function.

Example:

```ipython from agentscope.service.service_toolkit import * from agentscope.service.execute_code.exec_notebook import * nbe = NoteBookExecutor() code = “print(‘helloworld’)” # calling directly nbe.run_code_on_notebook(code)

>>> Executing function run_code_on_notebook with arguments:
>>>     code: print('helloworld')
>>> END

# calling with service toolkit service_toolkit = ServiceToolkit() service_toolkit.add(nbe.run_code_on_notebook) input_obs = [{“name”: “run_code_on_notebook”, “arguments”:{“code”: code}}] res_of_string_input = service_toolkit.parse_and_call_func(input_obs)

“1. Execute function run_code_on_notebook

[ARGUMENTS]:

code: print(‘helloworld’)

[STATUS]: SUCCESS [RESULT]: [‘helloworldn’]

__init__(timeout: int = 300) None[source]

The construct function of the NoteBookExecutor. :param timeout: The timeout for each cell execution.

Default to 300.

property cells_length: int

return cell length

async async_run_code_on_notebook(code: str) ServiceResponse[source]

Run the code on interactive notebook

run_code_on_notebook(code: str) ServiceResponse[source]

Run the code on interactive jupyter notebook.

Parameters:

code (str) – The Python code to be executed in the interactive notebook.

Returns:

whether the code execution was successful, and the output of the code execution.

Return type:

ServiceResponse

reset_notebook() ServiceResponse[source]

Reset the notebook