Google Colab variable inspector is overwhelmed by 'from sympy import *' - import

Google Colab has a feature called "variable inspector". The "variable inspector" pane shows the active variables in the Colab notebook. Unfortunately, when the import statement "from sympy import *" is inlcuded in the code file as below, then there are about thirty or forty variables dumped into the variable inspector. This is probs a bug as the purpose of the "variable inspector" is to permit the user to observe only the variables created by the user. Seems to me import statments should not be creating variables that populate the variable inspector? To observe this behavior, 1) start a new Google Colab notebook, 2) type "a = 3" in the code window and run the cell, 3) open the variable inspector pane with Tools>>>Command Pallette>>>variable inspector, 4) observe the single variable "a" in the newly opened inspector pane, 5) add the import statement "from sympy import *", and finally 6) observe all of the new and in my case unwanted variables in the inspector pane. The Colab cell code looks like this:
from sympy import *
a = 3
Seems to me that these variables loaded into the variable inspector should not be there. I should note that this issue is not a functional deficiency in Google Colab. Google Colab functions correctly. Its simply a matter of too much unimportant (to me) information displayed in the variable inspector pane.

Related

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 do I edit a jupyter notebook cell's tags in visual studio code?

I am editing a .ipynb file in Visual Studio Code, with the file open in VS code's Jupyter Notebook editor.
When I edit this notebook in the Jupyter Notebook App (i.e. if I were not using VS code, instead using the interface described here), I could add tags to cells by clicking View > Cell Toolbar > Tags, and then entering tags into the UI that comes up.
Is there an equivalent way to do this in VS Code?
I am aware I can reopen the file in a text editor view and edit the JSON directly. But I am looking for something a bit more user friendly than this.
Try installing this jupyter on vs code.
On my old windows 7 I used to use this. It has the same interface as when you open Jupiter in your browser but you don't need to load a bunch of stuff to open jupyter. You can open it in one single click like changing text file while writing code inside vc code.
Also you need to install one library to use it but unfortunately I can't remember the library as I don't use python or Jupiter anymore.
In the documentation is a python code cell which enables to change all the cell tags.
import nbformat as nbf
from glob import glob
# Collect a list of all notebooks in the content folder
notebooks = glob("**/*.ipynb", recursive=True)
notebooks_windows = []
for i in notebooks:
j = i.replace("\\", "/")
notebooks_windows.append(j)
notebooks_windows
# Text to look for in adding tags
text_search_dict = {
"# HIDDEN": "remove-cell", # Remove the whole cell
"# NO CODE": "remove-input", # Remove only the input
"# HIDE CODE": "hide-input" # Hide the input w/ a button to show
}
# Search through each notebook and look for the text, add a tag if necessary
for ipath in notebooks_windows:
ntbk = nbf.read(ipath, nbf.NO_CONVERT)
for cell in ntbk.cells:
cell_tags = cell.get('metadata', {}).get('tags', [])
for key, val in text_search_dict.items():
if key in cell['source']:
if val not in cell_tags:
cell_tags.append(val)
if len(cell_tags) > 0:
cell['metadata']['tags'] = cell_tags
Microsoft have finally released an extra extension to support this.
ms-toolsai.vscode-jupyter-cell-tags
See this long running github.com/microsoft/vscode-jupyter issue #1182 and comment recently closing the issue.
Be ware however, that while you can set tags, e.g. raises-exception, which is meant to make the runtime expect and ignore the exception and keep on running more cells, but the vscode-jupyter extension that runs the cells does not always honor tag features the same way that the standard Jupyter notebook runtime does, and so, disappointingly, it's still difficult to demo common mistakes or what not to do (exceptions).
Just copy everything in .py file with the unremovable tags, paste that into a plaintext file. The tags are now just editable text. Remove them, Save As or copy/paste back to the .py file.

Is there any way in vscode to know where the current the file has been imported?

Are there any extensions/features in vs-code which could let me know where the current file is imported? I usually do a global search of the filename but it is time consuming when the file name is similar to any variable or there are similar named files.
You can do a right-click on every function / variable or class. Then you choose "Find all references" to show where each function / variable or class is called.
For this you do not need an extension, because it is a standard feature of vscode
- find unused exports in project:
you can use this extension in vscode to find all unused exports
+ find name references (e.g. used exports) in project:
click on name then press shift+alt+F12
NO EXTENSION REQUIRED
Yes, We can find the references of a file in VSCODE like this:
Right-click on the file and then select Find References.
VSCODE will show all the references/imports of the file.
image reference
Right-click > Go to References (Shift + F12)
There is also an extension to change the appearance of how references show https://marketplace.visualstudio.com/items?itemName=jrieken.references-plusplus
If you are limiting your search to the current open workspace / folder, you can
do a search for 'import' using the magnifying glass or pressing ctrl+shift+s
open the results in editor by either clicking the link that says 'Open in editor' or by pressing alt+enter
In the document that opens, search for the name of your module.
In this way you will discover all of the imports that involve your module, regardless of whether you have used 'import mymodule' or 'from mymodule import myfunction' or anything else. I'm specifically thinking of how I'd structure a python project, but if you are using another language, you can alter the search in a way that works for the way your language does imports.

visual studio code add corresponding import statements for snippets

I tried to create custom snippets for my extension in vscode.
But to use those snippets I also need specific imports statements, I'm trying out to figure out how to add corresponding import statements while a snippet is selected from choices.?
Does vscode provide a way to do this?
If your programming language supports the "Optimize Imports" command, you can take advantage of it to get close to your desired behavior. By default it is set to the shift+opt+O keybinding in vscode.
In JS/TS the "Optimize Imports" command will move an import to the top of a file no matter what line it's written on, as long as it's a syntactically valid import, i.e., not inside a function, etc.
Option 1
You could make your snippet more convenient by ending it on the importable keyword. For example, with a React component snippet that you might need to import, you can use $0 to return to the keyword after supplying the additional content. This would allow you to immediately type cmd+. to "Quick Fix" the import.
{
"AuthorCard": {
"prefix": "ac",
"body": [
"<AuthorCard$0>",
" $1",
"</AuthorCard>"
]
},
}
Option 2
If your function snippet is intended to be inserted into the global scope of the file, then you can include the import in the snippet itself. Then, immediately "Optimize Imports" and it will send the import statement to the top of the file.
For example, the snippet could look like the following.
"blah": {
"prefix": "blah",
"body": [
"import blah from 'blah'",
"export function myBlah() {",
" return blah.doBlah()",
"}"
]
},
Option 3
If you snippet is intended to be used embedded within other scopes within the file, then create a second snippet of the just the import. E.g., for a snippet blah, the snippet iblah could import all the required dependencies. Now, you only need a "quick" way to get to a valid scope for the imports and then back to the place you started, both of which are possible in vscode. I will mention the default keybindings, but, for the record, you can rebind the underlying commands to whatever you'd like.
To get to a valid scope for your import snippet, you have multiple options. The best options is probably cmd+up, which will take you to the top of the file. Other options are shift+cmd+\, which takes you to the closing bracket of a statement, and cmd+enter, which takes you to a new line. From a valid scope you can trigger your import snippet and then "Optimize Imports" with shift+opt+O.
Lastly, to return to your original function, you can "Go Back" in vscode which defaults to ctrl+-, which returns your to your last cursor position.
These options might be less than ideal, especially if you are intending these to be public snippets for an extension package, but, still, they are a collection of convenient tricks that might provide useful (and answer the "spirit" of your question).

Matlab Calling 'Import Data' with GUI Button

I'm working in a GUI in Matlab R2014b that uses some data from a txt file, so i want to call the 'Import Data' window to permit the user select the data from a file when pushing a button. Is there a way to do this?
The function uiimport is used for importing data interactively. This seems to be what gets called by Matlab's toolbar's "import data" button.
Excerpting from the documenatation,
uiimport opens a dialog to interactively load data from a file or the clipboard. MATLAB® displays a preview of the data in the file.
uiimport('-file') presents the file selection dialog first.
S = uiimport(___) stores the resulting variables as fields in the struct S.
So, just set
uiimport('-file');
as the button's callback.