Issue
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
Solution
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.
Answered By - Wayne
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.