简体   繁体   English

如何覆盖 Python Jupyter 显示 output

[英]How to overwrite Python Jupyter display output

I have created a small form with ipywidgets.我用 ipywidgets 创建了一个小表单。 The sample code can be run in Jupyter or Google colab.示例代码可以在 Jupyter 或 Google colab 中运行。

Each time the form is filled and the button is clicked a row gets added to a dataframe.每次填写表格并单击按钮时,都会向 dataframe 添加一行。 Subsequently the dataframe gets displayed.随后显示 dataframe。

My problem is that the output displays the new updated dataframe on top of the old one.我的问题是 output 在旧的顶部显示新的更新的 dataframe。 What I want is that the new display output overwrites the old one.我想要的是新显示器 output 覆盖旧显示器。 See image description here .请参阅此处的图像描述

import ipywidgets as widgets
from ipywidgets import HBox, Label
from ipywidgets import Layout, Button, Box, FloatText, Textarea, Dropdown, Label, IntSlider
import time
import pandas as pd

#Create DF
df = df = pd.DataFrame(columns = ['Dropdown_column', 'Float_column'])
df

# Layout
form_item_layout = Layout(
    display='flex',
    flex_flow='row',
    justify_content='space-between',
)


button_item_layout = Layout(
    display='flex',
    flex_flow='row',
    justify_content='center',
    padding = '5%'
)

# Dropdown item

drop_down_input = 'Dropdown_input_1'

drop_down = widgets.Dropdown(options=[('Dropdown_input_1', 'Dropdown_input_1'), ('Dropdown_input_2','Dropdown_input_2'), ('Dropdown_input_3', 'Dropdown_input_3')])

def dropdown_handler(change):
    global drop_down_input
    print('\r','Dropdown: ' + str(change.new),end='')
    drop_down_input = change.new  
drop_down.observe(dropdown_handler, names='value')

# FloatText item

float_input = 0

FloatText = widgets.FloatText()

def IntText_handler(change):
    global float_input
    print('\r','Float text:' + str(change.new),end='')
    float_input = change.new 
FloatText.observe(IntText_handler, names='value')



# Button

button = widgets.Button(description='Add row to dataframe')
out = widgets.Output()
def on_button_clicked(b):
    global df
    button.description = 'Row added'
    time.sleep(1)
    with out:
      new_row = {'Dropdown_column': drop_down_input, 'Float_column': float_input}
      df = df.append(new_row, ignore_index=True)
      button.description = 'Add row to dataframe'
      display(df)
button.on_click(on_button_clicked)

# Form items

form_items = [         
    Box([Label(value='Dropdown'),
         drop_down], layout=form_item_layout),
    Box([Label(value='FloatText'),
         FloatText], layout=form_item_layout),
    Box([Label(value=''), button],
        layout=button_item_layout),    
]

form = Box(form_items, layout=Layout(
    display='flex',
    flex_flow='column',
    border='solid 1px',
    align_items='stretch',
    width='30%',
    padding = '1%'
))
display(form)
display(out)

I have tried using the print() function in combination with '/r' and changing #button part of my code.我尝试将 print() function 与 '/r' 结合使用并更改我的代码的#button 部分。 Change:改变:

display(df)

to

print('\r',str(df), end='')

or或者

print(str(df), end='\r')

But this does not work either.但这也不起作用。

Does somebody have any idea what to do?有人知道该怎么做吗?

\r works only for single line of normal text but df is not displayed as normal text (and it is not single line) but as HTML code. \r仅适用于单行普通文本,但df不显示为普通文本(也不是单行),而是显示为 HTML 代码。

You have to use out.clear_output() to remove previous content.您必须使用out.clear_output()删除以前的内容。

   with out:
      new_row = {'Dropdown_column': drop_down_input, 'Float_column': float_input}
      df = df.append(new_row, ignore_index=True)
      button.description = 'Add row to dataframe'

      out.clear_output()  # <---

      display(df)

You can see more about out.clear_output() in documentation:您可以在文档中查看有关out.clear_output()的更多信息:
Output widgets: leveraging Jupyter's display system Output 小部件:利用 Jupyter 的显示系统

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM