Unable to view executed cell code in Spyder's IPython console [duplicate] - ipython

I updgraded from Spyder 3 to Spyder 4 and now when I execute a cell I need to wrap a print() around whatever variable is in the last line of the cell so that the contents are automatically printed / displayed in the console. What setting do I need to revert in order to get back to the prior set up.
in case this is not clear:
display = "1 2 3 4 5"
display
{Ctrl Enter}
would ouput:
1 2 3 4 5
to the console
now it does not display anything in the console
Thank you.

What setting do I need to revert in order to get back to the prior set up?
You need to go to the menu
Tools > Preferences > Editor > Run Code
and activate the option called
Copy full cell contents to the console when running code cells.

Related

How to increase the number of lines being updated in VS Code Jupyter output?

VS Code has a setting called "notebook.output.textLineLimit": n, that controls the number of lines shown in the tab under the cell (by default, it's set to 30).
However, this number only affects the number of lines shown that are executed first, with the number of new output lines that are updated being limited to 4, regardless of this setting. They show up after a "..." line.
For example, if you run:
for i in range(0,100000):
print(i)
You will see the first n-4 (where n is your "notebook.output.textLineLimit" parameter) i's show up and then only the newest 4 i's be continuously printed out. See screenshot here
I looked at all settings that showed up when searching for 'notebook.' and for '#ext:ms-toolsai.jupyter' and didn't find the one that controlled the number of last lines printed out.

Jupyter Lab Max Number of Outputs Trimmed

How to change Jupyter Lab default behaviour trimming higher number of outputs.
The message in the middle of outputs says:
Output of this cell has been trimmed on the initial display.
Displaying the first 50 top and last bottom outputs.
Click on this message to get the complete output.
There is maxNumberOutputs parameter in Jupyter Lab source code, but I didn't found any method to change it.
You can change maxNumberOutputs in settings: click on Menu bar → Settings → Advanced Settings Editor → Notebook → set maxNumberOutputs in the User Preferences tab, like:
{
"maxNumberOutputs": 100
}
save, and reload.
Using jupyter lab path to find the "User settings" path, you can write from the notebook directly by executing this writefile magic from within a cell:
%%writefile <your-jupyter-lab-path>/user-settings/#jupyterlab/notebook-extension/tracker.jupyterlab-settings
{
"maxNumberOutputs":100
}
This path may be slightly different depending on your environment and Jupyterlab version, so probably best to manually change it using this answer and then finding the file that was modified. After that you can place this code before the %%writefile command to ensure that this works on your next Jupyter session without manually going to advanced settings in the menu bar:
!file="<your-jupyter-lab-path>/user-settings/#jupyterlab/notebook-extension/tracker.jupyterlab-settings" && mkdir -p "${file%/*}" && touch "$file"
Finally, to ensure that you have correctly changed the values,use this code to test your output:
from IPython.display import display
[display(i) for i in range(75)]
where 75 outputs should not be trimmed. If it is, then try refreshing the page to re-apply the settings.

How to display specific workspace to selected screen on i3 startup?

I have 8 different workspaces defined in ~/.i3/config:
set $workspace1_name 1:www
set $workspace2_name 2:programming
set $workspace3_name 3:communication
set $workspace4_name 4:files+dictionary
set $workspace5_name 5:documents
set $workspace6_name 6:graphics
set $workspace7_name 7:virtualization
set $workspace8_name 8:music
I also have 2 screens. When I start i3 on my Linux start-up, each screen shows a different workspace. The right screen is showing workspace 2:programming, but the left screen shows empty workspace 1 (not 1:www).
How can I configure i3 so that the left screen shows properly named workspace 1:www instead of 1?
According to the doc, the syntax is
workspace <workspace> output <output>
where output is the name of the RandR output you attach your screen to.
You can use one of the following RandR commands to get the output value
xrandr --current
# or if your X server supports RandR 1.5
xrandr --listmonitors
So, as an example (for my current dual screen set-up) the configuration should look like this
workspace "1: www" output DP-1

Is tab-switching a feature in VSCode?

By this I mean if some file was written with 4 spaces, can you simply highlight it all and click on something to turn it into 2 spaces. I'm not sure if in practice (parsing) this would make sense/could lead to broken code.
I currently have my editor.tabSize set to 2, and sometimes I open files written with 4 spaces and I want to be able to turn them into 2 spaces. I have at least figured out to turn off the auto-detect so that when I highlight sections of the code and hit shift-tab, then tab again it will turn the selected code from 4 spaces into 2 spaces.
Is there a feature like this or does it make sense that this wouldn't exist?
To change the current document from using 4 spaces to 2 spaces:
Click on Spaces: 4 in the status bar or run the Indent using Spaces command
Select 2 for the new tab size
Run for Format Document command to apply the new indentation to the entire document

Jupyter Notebook: command for hide the output of a cell?

In my notebook, I have a cell returning temp calculation results. It's a bit long, so after it is run, I want to hide it and when needed, to show it.
To do it manually, I can double click the left side of the output, to hide it
After double click
But is there any way I can do this by code? For example,
the last line of the cell, use a command like %%hide output, and the output would be hidden after finished running.
Additionally, can I get this feature in output HTML?
Add ; by the end of the cell to hide the output of that cell.
In the newer versions(5.0.0 at the time I'm writing this), pressing o in the command mode hides the output of the cell in focus. The same happens if you triple click in front of the output.
o is
the first letter in the word "output" or
lower case of 15th letter in the alphabet
You can add %%capture to the beginning of the cell.
Jupyter provides a magic cell command called %%capture that allows you to capture all of to outputs from that cell.
You can use it like this:
%%capture test
print('test')
test.stdout => 'test\n'
https://ipython.readthedocs.io/en/stable/interactive/magics.html
In newer versions of Jupiter Notebook, select the desired cell, make sure you're in command mode and then on the menubar press Cell > Current Outputs. You have then three options:
Toggle (press O in the command mode to apply the same effect)
Toggle Scrolling (the default output)
Clear (to clear the output all together)
Image to Menubar Options
Additionally, you can apply the same effect to all the cells in your document if you chose All Output instead of Current Output.
Not exactly what you are after, but the effect might be good enough for your purposes:
Look into the %%capture magic (https://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/notebooks/Cell%20Magics.ipynb). It lets you assign that cell output to a variable. By calling that variable later you could see the output.
Based on this, I just came up with this for myself a few minutes ago:
%%javascript
$('#maintoolbar-container').children('#toggleButton').remove()
var toggle_button = ("<button id='toggleButton' type='button'>Show Code</button>");
$('#maintoolbar-container').append(toggle_button);
var code_shown = false;
function code_toggle()
{
if (code_shown)
{
console.log("code shown")
$('div.input').hide('500');
$('#toggleButton').text('Show Code');
}
else
{
console.log("code not shown")
$('div.input').show('500');
$('#toggleButton').text('Hide Code');
}
code_shown = !code_shown;
}
$(document).ready(function()
{
code_shown=false;
$('div.input').hide();
});
$('#toggleButton').on('click', code_toggle);
It does have a glitch: each time you run that cell (which I put at the top), it adds a button. So, that is something that needs to be fixed. Would need to check in the maintoolbar-container to see if the button already exists, and then not add it.
EDIT
I added the necessary piece of code:
$('#maintoolbar-container').children('#toggleButton').remove()
You can use the notebook utils from https://github.com/google/etils:
!pip install etils[ecolab]
from etils import ecolab
with etils.collapse():
print('This content will be hidden by default')
It will capture the stdout/stderr output and display it a some collapsible section.
Internally, this is more or less equivalent to:
import contextlib
import html
import io
import IPython.display
#contextlib.contextmanager
def collapse(name: str = ''):
f = io.StringIO()
with contextlib.redirect_stderr(f):
with contextlib.redirect_stdout(f):
yield
name = html.escape(name)
content = f.getvalue()
content = html.escape(content)
content = f'<pre><code>{content}</code></pre>'
content = IPython.display.HTML(
f'<details><summary>{name}</summary>{content}</details>')
IPython.display.display(content)
The section is collapsed by default, but I uncollapsed it for the screenshot.
To prepend a cell from getting rendered in the output, in the notebook, by voilo or voila gridstack, just put in the first line of each cell to hide the output:
%%capture --no-display
reference in ipypthon documentation
For Windows,
in Jupyter Notebook, click the cell whose output you want to hide.
Click Esc + o for toggling the output
So I totally understand. When you have like 100 different plot and when you do the "Restart & Run All" those ugly plots all show up again
what you can do is ctrl+A and press o it will all of a sudden hide all your cells!!! For you to collapse automatically, you may need to use JupyterLab (another level after JupyterNotebook) but still, by doing ctrl+A then o you will be able to collapse all the results!!!
ctrl+A --> select ALL (make sure to click outside of coding box before you do it!)
o --> toggle collapse
If you don't mind a little hacking, then you may write a simple script for inverting the "collapsed" attribute of each cell from false to true
in the notebook .ipynb file (which is a simple JSON file).
This is however may fail in the future if a the .ipynb format changes.