How to input a string in IRuby + Jupyter? - sciruby

I'm using iruby on Jupyter. In python kernel (ipython), one can input a string from notebook by calling input() function.
Is it possible to achieve the same thing in iruby?

If you use Jupyter Notebook,
try
IRuby.input
https://github.com/SciRuby/iruby/tree/master/lib/iruby/input
But this input box may not work in Jupyter Lab.

Simple text input is done with IRuby.input:
name = IRuby.input 'Enter your name'
Multiple fields of input is done with IRuby.form:
result = IRuby.form do
input :username
button
end
It returns a Hash with the input field names as labels.

Related

Is there a way to insert N times the same characters

I can't find if vscode has a such failure. Is there a way to construct a string with N characters?
I explain myselft:
I need to wrote an empty string like this:
foobar = "1111111111111111";
There is 16 times characters '1'. Is there a way, like in Vim, to construct the line like this:
i wrote 'foobar = "' then i'd make a command to repeat 16 times the character 'i'.
I hope it's clear for you.
Here is an easy way using HyperSnips - a snippet extension that can use javascript to produce the output. First the demo:
The HyperSnips snippet:
snippet `"(.+)\*(\d+)=` "expand" A
``
let origStr = m[1];
let howMany = parseInt(m[2]);
let newStr = origStr.repeat(howMany);
rv=`"${newStr}`
``
endsnippet
This code goes inside a <yourLanguage>.hsnips file to have it run only in that language or all.hsnips to run in all files.
I made it to run inside a "" using this key: (.+)\*(\d+)=
The = is really the trigger - it runs automatically - you could change that to be something else. [The key could be shorter if you didn't care about digits being repeated.]
For more on setting up HyperSnips (which is pretty easy) see VSCode Advanced Custom Snippets
There currently is no native way, outside of copy/pasting.
You can use Repeat Paste:
Copies selected text and pastes repeatedly based on user input. Functions like Vim yank, paste, and repeat. For example, in Vim you would select the characters you want to copy then type 30p to paste the selected text 30 times.
Select a char and activate your command palette with CTRL + SHIFT + P and type 'Repeat Paste' and it will prompt you for the quantity.
You can assign a shortcut to this command
You can use the extension Regex Text Generator. You write a Regular Expression that generates your needed text
Type the following in the Generate Box
foobar = "1{16}";

Is it possible to use %edit in an IPython Notebook to edit in-browser?

I have been using the magic %edit in Ipython to edit strings on the fly and was wondering whether this is also possible in an IPython Notebook without having to leave the browser (Possibly in a new cell)?
It turns out that %edit does not load variable content in a new cell in jupyter notebooks for now (https://github.com/jupyter/help/issues/147).
However, you can load variable s contents in a new cell by using %load s. you can then edit the content and store it in a new variable from within the same cell.

Can I modify contents in Jupyter Notebook output cell?

In Jupyter notebook (FKA Ipython notebook), I have a cell which I ran for a long time until I got some text result in the output cell. However I found there is a small typo in the output that I would like to change without rerunning the cell again. Is there a way to modify contents in output cells?
The .ipynb files are just text files in JSON looking format. You can search for the typo and change it.
I suggest you fix the typo in the input cell in case you run it again and then save the notebook. Then open the .ipynb file and search for the error in an output cell and fix it. Save it as text and reopen in Jupyter. It will have changed.
Open the Jupyter notebook file in a notepad and change the output as you desire.
No, the output is entirely dependent on the input cell, so you will need to modify the input and run it again.

Can I create an iPython notebook using JavaScript as the language in the cells?

I love IPython to explain algorithms in python. But I want to do the same using javascript. Is it possible to write a notebook where I use javascript as the cell language?
You can use the %%javascript magic function for running javascript in IPython notebook. For example Paste the following code in a IPython cell and run it. You should see the output in your browser's javascript console.
%%javascript
console.log("Hello World!")
For global variables, you can add an attribute to the windows object for
example, in a cell run the following code:
%%javascript
window.myvar = 12;
In another cell, run the following code and check browser's javascript console. The variable's value should be printed.
%%javascript
console.log(myvar)
Use the element variable for printing in the cell output area as shown below:
%%javascript
element.append(myvar)

Handling carriage return/line feed in MATLAB GUI user controls

I have a MATLAB program I am developing in order to do some image processing stuff and need to use a user control into a MATLAB GUI user interface I created ad-hoc.
This user control is a List Box and there I would like to insert some text. Well the problem is not that I cannot put text there, I can do that using this call:
set(handles.mylistbox, 'String', 'MyStringToPrint');
Well the problem is that this call does not let me insert many lines in my list box but just overwrite the previous one.
I wish to find a way to let my code insert the new text in a new line. This should not be so difficult to do and it is also a simple pattern:
texttoprint = 'My text to add'
oldtext = get(handles.MyListBox, 'String') %Holding the previous text here
set(handles.MyListBox, 'String', [oldtext '\n' texttoprint]) %Setting (no line feed printed)
set(handles.MyListBox, 'String', [oldtext char(10) texttoprint]) %Setting (this fails too)
Well it is ok and it does not raise any error BUT, \n DOES NOT WORK.
I do not have any new line... BUT NEED TO!!!!
How should I solve this?
The problem is that I need to print text in this user control, not on the MATLAB commandline (that is very simple just by doing sprintf()).
What to do? Thank you
For a listbox, set the string property to a cell
set(myListboxHandle,'String',{'myFirstLine';'mySecondLine'})
If you want to add another line, call
contents = get(myListboxHandle,'String');
set(myListboxHandle,[contents;{'another line'}])
For multiline text in GUIs otherwise, use char(10) instead of \n, i.e.
set(someUiControlHandle,'String',sprintf('my first line%smy second line',char(10)))
When working with list boxes it's usually easier to deal with the options as a cell array of strings. So, you would initialize your list box as follows:
set(handles.MyListBox,'String',{'Option 1'});
And then you can add options to your list box like so:
newOption = 'Option 2';
oldOptions = get(handles.MyListBox,'String');
set(handles.MyListBox,'String',[oldOptions; {newOption}]);