# How to use same cell for printing in a loop?

I want to manually tag images.

For example, we prepare a list of images like

imgList = Table[Rasterize[i, ImageSize -> 50], {i, {a, b, c}}]


it gives

Now I made a loop like this

tagAssoc = <||>;
Do[
Print[imgList[[i]]];
tagAssoc[i] = Input[],
{i, 1, Length@imgList}];
tagAssoc


Basically, in each loop, it print a image, then popup an input window for me to input the description of the image. All tag information is saved in tagAssoc. After loop finished, it looks like below

The print order of images should be reversed for better view and tagging. But this is not my concern. Since I only need to see one image at a time, how to make this loop update the same cell with different images instead of print all images in different cell.

• Apparently you need Dynamic, not Print. Aug 4 at 5:01
• ..or use PrintTemporary? Aug 4 at 5:07
• @MikeHoneychurch Do you mean replace Print with PrintTemporary? But that does not work, it still outputs 3 images Aug 4 at 5:52
• @AlexeyPopkov I feel stupid, but could you show me how to do it? : ) Aug 4 at 5:53

One simple approach is to replace Print with Dynamic wrapped by PrintTemporary:

tagAssoc = <||>;
PrintTemporary[Dynamic[imgList[[i]]]];
Do[tagAssoc[i] = Input[], {i, Length@imgList, 1, -1}];
tagAssoc


... or even simpler, you can use Monitor instead as Lukas Lang suggests in the comments:

tagAssoc = <||>;
Monitor[Do[tagAssoc[i] = Input[], {i, Length@imgList, 1, -1}], imgList[[i]]]
tagAssoc

• Thank you so much for solution. One thing I do not understand. I thought i is local variable in Do loop, how can it affect outside i? Aug 4 at 6:50
• @matheorem if you look at the documentation of Do (specifically the second to last point in "Details"), you'll see that it effectively uses Block, which implements dynamic scoping. As you can see in the documentation, this means that the global value of i is temporarily changed. Aug 4 at 7:40
• @matheorem It's an interesting question. As I understand, during evaluation of Do the scoped variable is changed globally, but after leaving the Do scope the variable is set up to the value it had before intering Do. But I can be slightly wrong in the details. It is worth to ask a separate question on it. Aug 4 at 7:41
• @AlexeyPopkov you could probably use Monitor instead of manually combining PrintTemporary and Dynamic Aug 4 at 7:42
• @AlexeyPopkov Oh, it seems that someone already explain it mathematica.stackexchange.com/a/83043/4742 Fred Simons mentioned when Table or Do finished, it does not inform the front end to update Aug 4 at 9:46