How to Handle Tcl Treeview Selections - select

I am using the following procedure to delete a record within a database that is displayed within a treeview widget (z1):
set z1 [ttk::treeview .c1.t1 -columns {first last} -show headings]
proc Dlt {} {
global z1 z11
sqlite3 db test.db
db eval {
DELETE From t1 Where First_Name = $z11 and Last_Name = $z11
}
db close
}
$z11 in the sql statement should be the treeview selection. Unfortunately, I cannot figure out how to set a variable to equal the treeview selection. I can set a variable to equal the index, which is: set z11 [$z1 index [$z1 selection]]. This will give me the index of the treeview selection; however, I am trying to get the string value of the treeview selection.
Does anyone know what the correct syntax is to set a variable to equal a treeview selection?
Thank you,

To get the values for an item in the tree you would use the item subcommand of the tree. For example:
set selection [.tree selection]
set text [.tree item $selection -text]
This is all documented on the man page for the treeview widget.

As an aside, what platform are you working with? If Windows, for debugging purposes, you can add a "console show" command to your code to display an interactive console window. With that open, you can just use [puts] to display variable values. So, you could just use "puts $text" (from within your code) to see the value of your text variable.
Also, you can just type commands directly into the console for immediate evaluation. In many situations, spending a few minutes in the console can be very enlightening.
If you're not running under Windows, you don't even need the "console show" command, as anything written to stdout should appear in the original shell window.

Related

how to add different number at end of multi line edit?

Having trouble finding a way to do this, maybe it is not even possible?
In my case, for testing flow of if-statements/user-interaction, am temporarily adding 40 lines of console.log('trigger-fired-1'); throughout our code.
However, to tell them apart would like each to end with a different number, so in this case, numbers one to forty like so:
In the screen recorded gif, to replicate what I am going for, all I did was copy/paste the numbers one to nine. What I really would like is a shortcut key to generate those numbers at the end for me to eliminate that step of typing out each unique number.
Am primarily coding in Visual Studio Code or Sublime Text, and in some cases shortcuts are similar, or at least have same support but for different shortcut keys.
There are a few extensions that allow you to do this:
Text Pastry
Increment Selection
NumberMonger
For Sublime Text, the solution to this problem is the internal Arithmetic command. Something similar may or may not be available in VS Code (possibly with an extension of some sort) but I'm not familiar enough with it to say for sure.
This command allows you to provide an expression of some sort to apply to all of the cursor locations and/or selected text.
By way of demonstration, here's the example you outlined above:
The expression you provide is evaluated once for every selection/caret in the buffer at the time, and the result of the expression is inserted into the buffer (or in the case of selected text, it replaces the selection). Note also that when you invoke this command from the input panel (as in the screen recording) the panel shows you a preview of what the expression output is going to be.
The special variable i references the selection number; selections are numbered starting at 0, so the expression i + 1 has the effect of inserting the selection numbers starting at 1 instead of 0.
The special variable x refers to the text in a particular selection instead. That allows you to select some text and then transform it based on your expression. An example would be to use x * 2 immediately after the above example (make sure all of the selections are still present and wrapping the numbers) to double everything.
You can use both variables at once if you like, as well as anything in the Python math library, for example math.sqrt(i) if you want some really esoteric logs.
The example above shows the command being selected from the command palette interactively, where the expression automatically defaults to the one that you want for your example (i + 1).
If you want to have this as a key binding, you can bind a key to the arithmetic command and provide the expression directly. For example:
{
"keys": ["super+a"],
"command": "arithmetic",
"args": {
"expr": "i+1"
},
},
Try this one ...
its not like sublime
but works g
https://github.com/kuone314/VSCodeExtensionInsertSequence

Value from Variable View on Pydev is not complete

I tried to debug a python module, I saw in the Variables view that the value of a variable is not complete.
This ends with "...". I think this means that the string was truncated.
In the Details pane from Variable view I set to display the Max Length of the variable (0=unlimited) but also in the Details pane the variable ends with "...".
Why the variable don't have the full value?
Yes, this means that the string was truncated.
That value is hardcoded in pydevd -- it can be changed manually be editing eclipse/plugins/org.python.pydev.core/pysrc/_pydevd_bundle/pydevd_comm.py -- MAX_IO_MSG_SIZE in your install.
Now, usually this isn't a real problem because it's possible to just use the console during the debug session and print the variable in the console to view its full values (see: http://www.pydev.org/manual_adv_debugger.html -- search for console evaluation there).

Copying variable contents to clipboard while debugging in Visual Studio Code

I'm debugging in Visual Studio Code and I have a JSON object that I would like to copy as text to the clipboard.
Is this possible inside of Visual Studio Code?
I found two ways to do that, both of which are a bit hacky (in my eyes).
Use console.log
I think there will be a limit to the size of the string that this can output, but it was satisfactory for my requirements.
In the debug console, write console.log(JSON.stringify(yourJsonObject))
Copy the resulting output from the debug console. That can be a bit tedious for long strings, but a combination of mouse and keyboard (ctrl-shift-end) worked ok for me.
Use a watch (limited to 10'000 characters)
This method only works up to a limited size of the resulting json string (it looks like 10'000 characters).
Set a breakpoint in a reasonable location where your variable is in scope and start your app.
Go to the debug view, add a watch for a temporary variable, e.g. tmpJson
Get your breakpoint to hit.
In the debug console, write var tmpJson = JSON.stringify(yourJsonObject)
This will now have populated the watched variable tmpJson with the string representation of your json object
In the debug view, right click on the watched variable, click copy.
If the string is too long, it cuts it off with a message like the following:
...,"typeName":"rouParallel","toolAssembly":{"id":"ASKA800201","description":"CeonoglodaloD50R6z5","c... (length: 80365)"
But it would work for smaller objects. Maybe this helps some people.
It would be great to have this properly built-in with vscode.
There is an open issue regarding this: https://github.com/microsoft/vscode-java-debug/issues/624
Workaround :
Go to the VARIABLES panel and right click to show contextual menu on a variable
select Set Value
Ctrl+C
(tested on Java, not JavaScript)
I have an easy workaround to copy anything you want:
In the debug console, write JSON.stringify(yourJsonObject)
Copy the string without the double quotes " around the string
Open a browser, such as Chrome, open the inspecting tool, go on the console and write:
copy(JSON.parse("PASTE_THE_STRING_HERE"));
The object is now copy on your keyboard !
If you are debugging Python:
In the DEBUG CONSOLE type, for example:
import json
from pprint import pprint as pp
pp(json.dumps(outDetailsDict))
OUTPUT IS LIKE
{"": {"stn_ix": 43, "stn_name": "Historic Folsom Station (WB)", "name": "", },
...
The fastest way I found to do that on Visual Studio Code was
Adding a breakpoint where is located the object to copy
Right click on object and choose "Add to Watch"
From Watch sidebar, choose option "Copy Value" and it's all! 🎉
Note: this solution seems to work for longer values but not very long values. This answer suggests a 10,000 char limit and uses JSON.stringify(myvar) instead of just str(). On char limit, see also this comment below.
Tested in python debugger
Add the variable to Watch, but converted to string
str(myvar)
Right-click on the value of the watch, and select Copy Value
Now you should get the full value (but not for very long values. See note above).
(var name blurred out):
If you're in debug mode, you can copy any variable by writing copy() in the debug terminal.
This works with nested objects and also removes truncation and copies the complete value.
Tip: you can right click a variable, and click Copy as Expression and then paste that in the copy-function.
While the question presumably deals with JavaScript (JSON) based technologies, many people have posted Python-related posts in this thread. So I'll post a more specific answer for Python, although the reasoning can be extended with some tweaks to JavaScript-based technologies. 😊
Helper strategies for debugging with VSCode
Copying variable FULL VALUE (not truncated) to clipboard while debugging even for very long values and other additional strategies.
1 APPROACH: Access the "Run and Debug" screen and look for the "WATCH" area and double click inside it. Add something like str(<MY_VAR>) which should be the variable you want to find the value of during the debug process;
2 APPROACH: Access the "DEBUG CONSOLE" tab and in the footer (symbol ">") copy and paste the code below or another one of your choice...
def print_py_obj(py_obj, print_id="print_py_obj"):
"""Prints a Python object with its string, type, dir, dict, etc...
Args:
py_obj (Any): Any Python object;
print_id (Optional[str]): Some identifier.
"""
print(" " + str(print_id) + \
" DEBUG >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print(" 0 STR >>>>>>>>>>>>>>>>>>>>>>>>")
print(str(py_obj))
print(" 0 <<<<<<<<<<<<<<<<<<<<<<<<")
print(" 1 TYPE >>>>>>>>>>>>>>>>>>>>>>>>")
print(type(py_obj))
print(" 1 <<<<<<<<<<<<<<<<<<<<<<<<")
print(" 2 DIR >>>>>>>>>>>>>>>>>>>>>>>>")
print(str(dir(py_obj)))
print(" 2 <<<<<<<<<<<<<<<<<<<<<<<<")
print(" 3 DICT >>>>>>>>>>>>>>>>>>>>>>>>")
try:
print(vars(py_obj))
except Exception:
pass
print(" 3 <<<<<<<<<<<<<<<<<<<<<<<<")
print(" " + str(print_id) + \
" DEBUG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
print_py_obj(profit, "<MY_VAR_OPTIONAL_IDENTIFIER>")
NOTE: We may have a 10,000 character limitation for both approaches.
Thanks! 🤗🐧🐍
Yup ! That's very much possible
Select breakpoint near your json variable.Debug.
screnshot . Right click on json body and copy value.That's it.
On the selected breakpoint, go to the Debug Console and print(variable)

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.

Print query results when using format=alinged, instead of open them in EDITOR

If a do a select and only retrieve a few columns, the results are printed to the terminal, if I have more columns, and they do not fit in the terminal width, the query results are opened in the default editor (vim), but when I exit the editor the results are no longer visible.
I know I can user \x (but I have many rows, and for my it's seems worst ).
If I change the format to unaligned, html, latex or troff-ms even if the results are wider then the terminal width they are still printed.
When the resulted rows do not fit in the terminal height they are always opened in the default EDITOR, no mater what format I am using.
Q:
There is any posibility to use format=aligned and allways print the results instead of opening them in the default EDITOR, so I will not loose their visibility (something similar to what mysql-client does) ?
Thank you.
The query results are passed to the PAGER program when they don't fit in the screen, unless it's disabled with \pset pager off. EDITOR is used for input.
Some pagers restore the previous display when they quit, and it can be quite irritating when you need to use previous results in further queries.
I've found PAGER="less -FX" to be a good fit with psql since it allows scrolling in both directions and keeps the display intact when it quits.