Execute/Run a single line in IPython rather entire Cell - ipython

I have often this problem, when I'm slicing or subsetting data that I want to view/print [df.head()] the data and look into into it before writing next line of my code.
For this case, every time, I have to run the whole block(cell) in ipython, even if I have some logic written I had to comment that block and execute my print line alone.
Is there a feature where you can select a single line and execute it.

I often start a qtconsole attached to the kernel. You can do that as follows:
Create a new cell.
In the new cell, type %qtconsole and execute that cell.
Delete the new cell.
Once you have a qtconsole that is attached to the notebook kernel. You can print
the value of variables there.

Related

How to make org-mode display results in real time?

Whenever I run a source block in emacs org mode, the whole block executes and then I see the results. Is there a way to see the output of the program in real time?
For instance, when installing packages with pip install, the progress bars appear all at once, and they appear one after another (so org mode is not interpreting carriage returns correctly). Is there a header argument or some variable I can set to fix this? If not, where could I insert a filter function to achieve the same result?

Julia history storage with command line outputs

Is there a method to store the command line outputs to a file in Julia.
The command history can be seen at .julia\logs\repl_history.jl in Windows OS.
I wish to store command line output also to a file automatically.
? command outputs also.
Whatever is shown in the julia command line, the whole text should be stored into a file.
Is there a way to do this.
found a similar qn : For Julia saving output to txt/CSV file?
If you run Julia in REPL mode in Widows using PowerShell, you can do the following in PowerShell:
Open the left upper corner menu and in Defaults, change the buffer size to something large enough for your needs, say 500.
After running Julia to do what you need, go the the left hand corner menu and choose Edit - Select all, then Copy. Paste to an editor.

IPython function definition indentation

Is there a configuration option to make ipython history bring up the entire previously entered function block, rather than doing it line by line?

How do I reset the Jupyter/IPython input prompt numbering?

I just wrote my first extensive Python tutorial using IPython notebooks. All went well, except I did a lot of testing and moving blocks around. How do I reset the In [ ]: numbering? I have tried quitting and reloading, but that doesn't seem to work.
I think, the only way to to what you want is:
- 'Kernel > Restart' (restart the kernel) and then 'Cell > Run All' (run the script).
Every .ipynb file can be opened in an editor. Everything written there is in plain text (JSON). For each cell which has the "cell_type": "code" there'd be another key-value pair as "execution_count": <number>. As you might have guessed, that is the prompt numbering. Hence, if the notebook contains code which will take time to execute (as was, in my case) this method would be time efficient.
Now, either you can manually change each execution_count or write a simple script to get the numbering right. To check the results just refresh the notebook in the browser without stopping the kernel. And, everything will be as per your needs, even all the variables/loaded data will remain in the environment.
You can reset the kernel (shortcut: C-m .) and re-run the whole notebook.
Quitting and reloading doesn't work because the code is not re-evaluated.
'Kernel' -> 'Restart & Run All'
Just make sure you saved your Notebook. You can also bind/assign keyboard key for running this command.
'Help' -> 'Edit Keyboard Shortcuts'
If what you want is to remove the numbers themselves, so that each cell shows In [ ] (instead of something like In [247] which is leftover from some previous incarnation of the kernel), use "Cell" > "All Output" > "Clear" (in Jupyter Notebook 5.4.0) or "Edit" > "Clear All Outputs" (In Jupyter Lab 0.32.1).
This will remove all the numbers, even if you're in the middle of running a notebook. It will not reset the numbering back to 1; e.g. if the last cell you executed was 18, the next will be 19.
If you're using this because you want clarity about which cells you've executed during this run of the kernel and which cells you haven't executed yet, use "Cell" > "All Output" > "Clear" (or "Edit" > "Clear All Outputs") immediately after you start (or restart) the kernel. This can be useful when restarting a kernel, or when opening a saved or duplicated notebook.
This will also remove all outputs from the notebook.
Thanks to user2651084 in a previous comment for this.
I'm a bit too late, but I had the same problem, and since my notebook had cells with execution time up to 5 minutes, I had to wait a long time until Restart & Run All finished.
So I've made a Python script to make this task for me:
import json
file = '/your/notebook/path/Notebook.ipynb'
# Since every notebook is actually a JSON (JavaScript
# Object Notation), then its contents can be represented
# in a dictionary (or a list of dictionaries)
with open(file, encoding='utf-8') as f:
nb = json.load(f)
count = 1
for cell in nb['cells']:
# Markdown cells doesn't have execution count,
# so apply this only to cells that have one
if 'execution_count' in cell:
cell['execution_count'] = count
count += 1
# Not all code cells have output, such as functions
# that return None or simple declarations, so apply
# this only to cells that have some output
try:
for output in cell['outputs']:
if 'execution_count' in output:
output['execution_count'] = cell['execution_count']
except KeyError:
continue
with open(file, 'w+') as f:
json.dump(nb, f, indent=2, ensure_ascii=False)
But be careful with the execution order and the variables in your cells, since applying the script above on your notebook can generate a different output if you run the notebook again. For example, let's suppose your notebook have the following cells with the execution order in square brackets:
In [2]: a = 1
In [1]: a = 2
In [3]: a
Out[3]: 1
If you apply the above script into your notebook, it'll show the following:
In [1]: a = 1
In [2]: a = 2
In [3]: a
Out[3]: 1
But if you run the notebook again, it'll show the following:
In [1]: a = 1
In [2]: a = 2
In [3]: a
Out[3]: 2
This can be a bit confusing for people who are downloading your notebook via GitHub for example, since they can see an output in the repository, but when they run on their machine, the output will be different.
Cell > All Output > Clear Clear all In []: numbers but do not reset them back to 1 for the next cell you run.
Kernel > Restart & Clear Output Restart the kernel, clear output, clear In []: numbers and reset them to 1, and clear output.
Restart & Run All isn't a good solution, because simply I don't want to run all (and that's the purpose of a notebook to run things cell by cell).
Anyways, I found this solution more plausible:
Main Menu > Cell > All Output > Clear
For those coming from Google:
%reset
This is useful when you want to reset all variables after a certain point in the notebook. It is going to ask if you are sure that you want to reset. If you want to force reset without asking, then use:
%reset -f
Here is how to clear the execution numbers to [ ] without re-running the whole notebook:
Just to be safe, make a copy of your notebook:
cp notebook.ipynb notebook_copy.ipynb
Open your notebook with vim:
vim notebook.ipynb
In vim, reset the execution numbers with the following search-replace command:
:%s/"execution_count":.*/"execution_count": null,/gc
You will then get the following prompt. Type a to replace all occurrences:
replace with "execution_count": null, (y/n/a/q/l/^E/^Y)? a
Save notebook and quit vim:
:wq

Disable auto-scrolling in command window

A lot of the code that I write in Matlab has a very verbose output. As the program runs, information is printed to the command window, and with each new line, the window automatically scrolls to the bottom. This becomes a problem when I want to read some of the output more closely or scroll up to look at older output. I can scroll up, but only until a new line is printed, which is often less than a second.
Does anyone know if it is possible to turn off this automatic scrolling in the Matlab window? I work in a number of different Matlab versions, depending on the machine, and this happens with all of them. The answer to this might be "No", but I swear I remember having this functionality at one point.
Use the more function: http://www.mathworks.com/help/matlab/ref/more.html
more on
Then run your program. Press spacebar when you wish to see more of the output.
more off will turn it off.
You may find this workaround useful.
First launch matlab using the command line matlab -logfile 'myLog.txt' (the doc says it "starts MATLAB and makes a copy of any output to the Command Window in filename. This includes all crash reports.")
Then open your .txt file using a text editor supporting automatic refresh of content (see picture). On OSX I use TextWrangler (freely available at www) but others have been reported to have this feature (see here or here).
Results: output displays (fprintf, disp, but not the commands per se) are printed both on the Matlab console and the text editor (file is refreshed with a little lag time, below half a second I would say with my configuration). And there is no automatic scrolling. Such procedure does not seem to impact the overall performance of the script (although it may deserve some testing).