1
$\begingroup$

Question

While working with Python's ExternalEvaluate the intermediate calculation results with Python are stored in the notebook. (ExternalEvaluate seems to cache evaluation results.)

How can I remove that cached data from the notebook?
(Say, before sharing with others.)

(I tried the commands here and others, without success...)


Code to reproduce "caching effect"

1) Make a notebook

Create/enter the following Python cells:

samples=[]
samples.append("".join(["a","B","C","73","e"][1:4]))
len(samples)

[len(x) for x in samples]
def some_func(x):
    return x / 100
some_func("yyu")

And save the corresponding notebook.

You should get something like this:

enter image description here

2) Find cached values

Open the notebook as text or in a text editor and search for the string "BC73".

(You should be able to find it, but I do not think one should, or at least, there should be a way to opt out.)

You can also find peculiar execution data or paths of your environment.


Additional comments

So, I can answer my question as:

Remove failure objects of Python external evaluations.

But I am curious are there any better and/or more "bureaucratic" solutions.

The reason I am making "all that fuzz" is because in the way described above one can inadvertently and easily share certain access and/or private information.

(In my case, accessing certain cloud services.)

$\endgroup$
7
  • 2
    $\begingroup$ What exactly do you mean by "caching intermediate calculation results"? For example, if I do some simple ExternalEvalute with python, there seems to be no sign of any caching in the document. It would help if you give a concrete example of how to reproduce your issue ... $\endgroup$
    – Domen
    Oct 4, 2023 at 12:35
  • 1
    $\begingroup$ I cannot see any cached results in my notebook in which I used python. Can you post what is in your notebook that you think is cache produced by ExternalEvaluate? $\endgroup$ Oct 4, 2023 at 12:42
  • $\begingroup$ @azerbajdzan and Domen -- Thank you for your comments! You are right -- using regular, non-problematic Python code does not produce the caching effects I observed. I am going to explain in more detail in a post. $\endgroup$ Oct 4, 2023 at 15:37
  • $\begingroup$ I do not think it is any cache. It is simply the output cell produced by evaluating python code. when you delete the output cell and save notebook again the thing you claim is cache disappear from the file. $\endgroup$ Oct 4, 2023 at 16:04
  • 1
    $\begingroup$ @Anton Antonov: Maybe I found a way to automatize it... see my answer. $\endgroup$ Oct 4, 2023 at 16:49

1 Answer 1

1
$\begingroup$

This delete all cells in the notebook that contain "Failure". To prevent the code to delete itself I had to write "Failure" as "Fail" <> "ure" otherwise also this cell would be deleted.

NotebookFind[EvaluationNotebook[], "Fail" <> "ure", All, CellContents]
NotebookDelete[EvaluationNotebook[]]

Maybe you can run it as last cell code also with saving notebook like this:

NotebookFind[EvaluationNotebook[], "Fail" <> "ure", All, CellContents]
NotebookDelete[EvaluationNotebook[]]
NotebookSave[EvaluationNotebook[]]

Or instead of "Failure" you can use "PythonError" in the form "Python"<>"Error" to be more specific since Failure is used by Mathematica and it could lead to deleting unwanted cells.

$\endgroup$
2
  • $\begingroup$ Agreed, using "PythonError" is better. $\endgroup$ Oct 4, 2023 at 17:12
  • $\begingroup$ Thank you for your post. $\endgroup$ Oct 6, 2023 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.