Jupyter和Spyder的行为不同。

huangapple go评论56阅读模式
英文:

Jupyter and Spyder behaviors are different

问题

1- 有人可以解释为什么会有这种行为差异吗?
2- 我可以设置Spyder的行为与Jupyter notebook相同吗?
谢谢

英文:

I have the following data as an example:

data = (
        [1,230.1,37.8,69.2,22.1],
        [2,44.5,39.3,45.1,10.4],
        [3,17.2,45.9,69.3,9.3],
        [4,151.5,41.3,58.5,18.5],
        [5,180.8,10.8,58.4,12.9],
        )

I use the same code on Spyder and Jupyter notebook, but I receive a different output, e.g.:

If I enter data (without print()) in Spyder, I receive no output, just new line, but if I enter data (without print() as well) in Jupyter, I receive the full data in the output

([1, 230.1, 37.8, 69.2, 22.1],
 [2, 44.5, 39.3, 45.1, 10.4],
 [3, 17.2, 45.9, 69.3, 9.3],
 [4, 151.5, 41.3, 58.5, 18.5],
 [5, 180.8, 10.8, 58.4, 12.9],
 )

The only way to get the output in Spyder is to use the print() command

print(data)



([1, 230.1, 37.8, 69.2, 22.1], [2, 44.5, 39.3, 45.1, 10.4], [3, 17.2, 45.9, 69.3, 9.3], [4, 151.5, 41.3, 58.5, 18.5], [5, 180.8, 10.8, 58.4, 12.9])

Also you can see the difference between the output format in both cases

1- Can someone please example why the difference in behavior?

2- Can I set up Spyder to behave the same way Jupyter notebook is behaving?

Thanks

答案1

得分: 0

关于您帖子底部的'1':

我认为要解决#1的主要摘要是在IPython和Jupyter中,最后一行很特殊。IPython/Jupyter将尝试以生态系统中最佳方式表示在输入的代码块(IPython)或单元格(Jupyter)中的最后一行。 (它来自REPL模型的'- print -'部分。它将尝试“打印”最后一行是什么。我特意在此处加了print一词,因为在Jupyter模型中,“print”可能意味着以漂亮的方式表示它,因为它在接口上具有更多功能。)Python没有这个功能,因此您必须告诉它如何处理您认为是结果的内容,方法是指定如何显示它。通常,您只需像在您的示例中一样使用print()。 (我在您的帖子中添加了一些关于在生态系统的上下文中理解事情以及在每种情况下可以期望什么的评论。)
其中一个最大的区别是Pandas数据框的处理,如果您已经遇到它们。Jupyter对它们有很好的表示,而无需您做任何事情,部分原因是因为它在浏览器中。在Python中,您必须告诉它如何以及具体要显示数据框,而且在基于文本的接口中,它将纯粹以文本为基础。
对于Jupyter而言,绘图是另一个类似的情况,它可以在界面中很好地显示它们。但是,最近Jupyter的处理方式发展得更进一步,因此不再需要在最后一行触发绘图的“打印”样式处理,因为现在Jupyter将尝试显示在Jupyter单元格中制作的绘图对象,如果它正确检测到已制作了一个。 (您将看到很多过时的代码建议您需要通过引用绘图或类似plt.show()的内容在最后一行上调用它,有时在故障排除时尝试这样做是很好的,但现在大多数情况下都不再需要。因此,它在输出处理的范式中会产生一些混淆,因为最后一行变得重要。)相比之下,在Python中,您必须设置一些内容来处理绘图的显示,例如安装Qt以触发将其显示到Qt窗口,或使用您的Python代码将绘图对象发送到图像文件,然后像查看任何其他图像文件一样查看系统上的图像文件。

关于您帖子底部的'2':

请查看the Spyder-notebook插件

英文:

In regards to '1' at the bottom of your post:

I think the main summary to address #1 is that in IPython and Jupyter, the last line is special. IPython/Jupyter will try to represent what is on the last line in an entered block of code (IPython) or cell (Jupyter) as best considered in the ecosystem. (It comes from the '- print -' part of the REPL model. It will try to 'print' what the last line is. I am purposefully putting the word print in quotes here as 'print' in Jupyter model can mean represent it nicely because it has more abilities in the interface.) Python doesn't have that feature so you have to tell it how to handle what you consider the result by specifying how to display it. Usually you just use print() as you do in your example. (I added some information in my comments to your post on the evolution that may help you understand things in the context of the ecosystem and what to expect in each situation.)
One of the biggest differences is Pandas dataframes handling, if you've come across them. Jupyter has nice representation of them without you needing to anything & party that is only possible because it is in a browser. In Python you have to tell it how and what specifically to display in regards to the dataframe, and it will just be purely text-based in the text based interface.
Plots are another similar one for Jupyter where it displays them nicely right in the interface. However, handling these has evolved further in Jupyter recently so that no longer does this have to be invoked on the last line to trigger 'print'-like handling for the plot, as these days Juptyer will try to display a plot object made in a Jupyter cell if it properly detects one has been made. (You'll see a lot of outdated code suggesting that you need to invoke showing it on the last line by referencing the plot or something like plt.show() and sometimes it is good to try that when troubleshooting, but mostly it is not needed now. So it kind of confuses the paradigm of output handling with the last line being important.) By contrast, in Python you have to have something set up to handle displaying a plot, such as installing Qt so it will trigger displaying it to a Qt window or use your Python code to send the plot object to an image file, and then view that on your system as you would view any other image file.

In regards to '2' at the bottom of your post:

Check out the Spyder-notebook plugin.

huangapple
  • 本文由 发表于 2023年2月18日 15:12:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定