问题
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