How to clear the output in a Jupyter Notebook cell after each for loop iteration ?

Published: May 22, 2023

Tags: Python;

DMCA.com Protection Status

In a Jupyter Notebook, you can use a for loop to iterate over items in a sequence. For example, you could use it to print out each item of the list one at a time. However, after running the code in the cell containing the for loop, all of the output is printed onto the same line and may be difficult to read. To clear the output from each iteration of your for loop and make it easier to understand what's happening, you can add an additional line to your code that uses the IPython.display.clear_output() method. This will clear any previous output before printing out each new iteration so that only one item is shown at once instead of everything appearing together. To do this, start by importing the IPython.display module and then add a line that calls the clear_output() method after each iteration of the for loop:

Using clear_output() method

from IPython.display import clear_output

for i in range(3):
    username = input('Enter Username')
    print(i, username)
    clear_output(wait=True)

By adding this additional line to your code, each iteration of the for loop will clear any previous output before printing out the new item from the list. This allows you to better visualize what's happening as your code is running and makes understanding your results easier. It also helps make sure that all of your output is visible on screen instead of having everything

Example

Below is an example where we iterate through several images and ask if they contain a solar panel farm

import folium
import pandas as pd
import numpy as np

from IPython.display import clear_output

data = np.array([[349, 3820, 27.839248657226562, 116.39884185791016, 367.0,
        287.5710754394531, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        15.865395545959473, 1.8201713562011719, 0.568378746509552, 1, 0,
        0, 7, 1, 45.845672607421875, -154.86322021484375,
        18.851022720336914, 80.26931762695312, 0, 'Day',
        0.523607075214386, 1.0820577144622803, 1.600000023841858],
       [649, 3823, 28.80352210998535, 116.09571838378906, 367.0,
        297.1990051269531, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        10.88183879852295, 1.5843127965927124, 0.7265815734863281, 0, 0,
        0, 7, 1, 46.636558532714844, -155.5345001220703,
        18.935832977294922, 78.32439422607422, 0, 'Day',
        0.5620156526565552, 1.0661375522613525, 1.600000023841858],
       [785, 3784, 29.274351119995117, 116.13337707519531,
        344.3221130371094, 295.54931640625, 305.1109924316406,
        295.8750915527344, 9.235969543457031, 3.505326271057129,
        1.9297815561294556, 1.5755447149276733, 14.316523551940918,
        2.989433526992798, 0.6907815933227539, 0, 0, 10, 8, 1,
        47.089637756347656, -155.63401794433594, 17.746267318725586,
        79.08007049560547, 0, 'Day', 0.8018679022789001,
        1.5472339391708374, 1.267532467842102],
       [787, 3784, 29.281211853027344, 116.13185119628906,
        354.74627685546875, 297.3043212890625, 305.18701171875,
        295.8792724609375, 9.307778358459473, 3.5285837650299072,
        1.9626555442810059, 1.5659282207489014, 13.985917091369629,
        4.0554094314575195, 0.6870543360710144, 0, 0, 10, 8, 1,
        47.095340728759766, -155.63851928710938, 17.746490478515625,
        79.26925659179688, 0, 'Day', 0.5754696130752563,
        1.2988111972808838, 1.517224907875061],
       [927, 3836, 29.697757720947266, 115.77227020263672, 367.0,
        295.2755432128906, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        7.547194480895996, 2.4319303035736084, 0.6598378419876099, 1, 0,
        0, 7, 1, 47.35850143432617, -156.20074462890625,
        19.34339141845703, 80.09915924072266, 0, 'Day',
        0.7618721723556519, 1.1604864597320557, 1.600000023841858]],
      dtype=object)

dfsp = pd.DataFrame(data=data, columns = ['FP_line',
 'FP_sample',
 'FP_latitude',
 'FP_longitude',
 'FP_T4',
 'FP_T5',
 'FP_MeanT4',
 'FP_MeanT5',
 'FP_MeanDT',
 'FP_MAD_T4',
 'FP_MAD_T5',
 'FP_MAD_DT',
 'FP_power',
 'FP_Rad13',
 'FP_MeanRad13',
 'FP_AdjCloud',
 'FP_AdjWater',
 'FP_WinSize',
 'FP_confidence',
 'FP_day',
 'FP_SolZenAng',
 'FP_SolAzAng',
 'FP_ViewZenAng',
 'FP_ViewAzAng',
 'FP_PersistentAnomalyCategory',
 'Day_Night_Flag',
 'T1',
 'T2',
 'T3'])

for index, row in dfsp.iterrows():

    m = folium.Map(location=[0, 0], zoom_start=17)

    long = row['FP_longitude']
    lat = row['FP_latitude']

    print(long,lat)

    m = folium.Map([lat,long], zoom_start=17)

    folium.TileLayer(
            tiles = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
            attr = 'Esri',
            name = 'Esri Satellite',
            overlay = False,
            control = True
           ).add_to(m)

    folium.Marker(
        location=[lat, long],
        popup="Mt. Hood Meadows",
        icon=folium.Icon(icon="cloud"),
    ).add_to(m)

    display(m)

    value = input('Input a value. Type x to quit.')

    clear_output(wait=True)

How to clear the output in a Jupyter Notebook cell after each for loop iteration ?
How to clear the output in a Jupyter Notebook cell after each for loop iteration ?

References

Links Site
display ipython.org
Image

of