Как я могу предотвратить запуск ячейки, если в оболочке IPython выполняются некоторые условия?Python

Программы на Python
Anonymous
Как я могу предотвратить запуск ячейки, если в оболочке IPython выполняются некоторые условия?

Сообщение Anonymous »


Предположим, я запускаю следующий код в интерактивной оболочке IPython:

Код: Выделить всё

def pre_run_cell(info):
raise RuntimeError("please don't run")

get_ipython().events.register("pre_run_cell", pre_run_cell)
Then, each time before a cell is run,

Код: Выделить всё

pre_run_cell()
is called.
However, even though the

Код: Выделить всё

pre_run_cell()
raises an error, the cell is still executed:

Код: Выделить всё

In [3]: 1
Error in callback  (for pre_run_cell), with arguments args (,),kwargs {}:
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[1], line 2, in pre_run_cell(info)
1 def pre_run_cell(info):
----> 2         raise RuntimeError("please don't run")

RuntimeError: please don't run
Out[3]: 1
How can I check some condition and possibly prevent the cell from running before running each cell? (or from within

Код: Выделить всё

pre_run_cell
hook)
I don't see anything in the documentation of

Код: Выделить всё

pre_run_cell
) with such a feature.


Источник: https://stackoverflow.com/questions/779 ... fied-in-ip

Вернуться в «Python»