jupyter input,display, print excute order is chaotic

社会主义新天地 提交于 2019-12-08 12:48:03

问题


I'am using jupyter and my python version is 3.5. In a while loop, excute order is not correct, input statement is before print statement. This is my code.

from IPython.display import display
import pandas as pd
df = pd.DataFrame({'a':[1,2],'b':[3,4]})
while(True):
    a = input("please input:\n")
    display(df.head())
    print (a)

The excute result is


回答1:


I was able to reproduce the behavior under Chrome 63 on OSX. I added several more consecutive print(a) statements and where the input field ends up is random: before them, after them, or in between. My suspicion is that each display and print call sends a request to the server but awaits its result asynchronously, so that input may be called again before the result from print(a) is ready.

It is not an elegant solution, but adding a small sleep (time.sleep(.02)) after print(a) fixes the problem for me.




回答2:


This is a known Jupyter issue:

https://github.com/jupyter/notebook/issues/3159



来源:https://stackoverflow.com/questions/48198676/jupyter-input-display-print-excute-order-is-chaotic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!