Notebook in VSCode displays no output for some stored variables - python
Consider the following code:
import pandas as pd
import os
csv = """Country,Total Cases,New Cases,Total Deaths,New Deaths,Recovered,Serious
China,74187,1751,2006,138,14796,12017
Diamond Princess,621,79,,,17,20
Singapore,81,,,,29,4
Japan,80,6,1,,20,4
Hong Kong,63,1,2,1,5,6
S. Korea,51,20,,,16,
Thailand,35,,,,17,2
USA,29,,,,3,
Taiwan,23,1,1,,2,
Malaysia,22,,,,15,
Vietnam,16,,,,14,
Germany,16,,,,9,
Australia,15,,,,10,
France,12,,1,,7,
Macao,10,,,,5,
U.A.E.,9,,,,3,1
U.K.,9,,,,8,
Canada,8,,,,1,
Philippines,3,,1,,2,
Italy,3,,,,,2
India,3,,,,3,
Russia,2,,,,2,
Spain,2,,,,2,
Nepal,1,,,,1,
Belgium,1,,,,1,
Sri Lanka,1,,,,1,
Finland,1,,,,1,
Egypt,1,,,,,
Cambodia,1,,,,1,
Sweden,1,,,,,"""
with open("data/covid.csv", "w") as file:
file.write(csv)
df = pd.read_csv("data/covid.csv").fillna(0).astype(dtype = int, errors='ignore')\
.sort_values(by='Total Cases', ascending=False)
latest = df.to_dict('list')
some_var = latest.keys()
print(latest.keys()) # It's a notebook. Printing or calling a variable does not matter.
latest.keys()
Please see the attached image to see the issue
It works in a jupyter notebook on conda and if I run it via a normal python file. Which highlights that the issue lies with how VSCode displays notebook outputs
As you can see, VSCode does not display some outputs after running a cell, but does display others after running a cell.
If I try this in a conda environment, it works just fine:
Any suggestions for why this issue only happens in VSCode & How I might go about fixing it? Is it a kernel thing?
This issue was caused by an extension within vscode called Leaflet Map.
It changed the default renderer for notebook cells in vscode.
If anyone else has issues with VSCode outputs in future, you can click the elipsis on the output cell -> Click Change Presentation & ensure you're using an up to date renderer:
Related
Force Jupyter to print and not display
I am using Jupyter notebooks in Visual Studio Code to present lab sessions for my Python programming course, albeit I am recommending students to work with .py files for themselves and their submissions. Ideally I would like the output of my Python cells to be identical to a Python console, and not pretty-printed by display. I have already added "jupyter.runStartupCommands": [ "from IPython.core.interactiveshell import InteractiveShell", "InteractiveShell.ast_node_interactivity = 'all'" ] to settings.json to ensure I get all output but it is still passed to display. However, in a Python console, running int shows the representation <class 'int'>, whereas in the notebook it just shows int. Is there a way to force the Python cells to print and not display, so that the output is identical?
Notebook validation failed Jupyter
This is not a duplicate of Notebook Validation Failed. I have a Jupyter ntb which was working well until recently. Whatever I do, I am getting those kind of errors which are changing (I'm attaching a few examples) and it does not matter what I do with the ntb (I tried restarting kernel, restarting Jupyter). Also, it is happening ONLY in this ntb, not in others even if run at the same time from the same session. I tried to search but could not find anything. What I get in the Jupyter is: There are many of those, similar but different: The save operation succeeded, but the notebook does not appear to be valid. The validation error was: Notebook validation failed: Non-unique cell id 'geological-poker' detected. Corrected to 'front-hampshire'.: "<UNKNOWN>" or others: Notebook validation failed: Non-unique cell id 'medieval-nebraska' detected. Corrected to 'stock-eating'.: "<UNKNOWN>" Notebook validation failed: Non-unique cell id 'intense-award' detected. Corrected to 'blocked-garage'.: "<UNKNOWN>" And what I get in the terminal is: Notebook JSON is invalid: Non-unique cell id 'medieval-nebraska' detected. Corrected to 'convinced-vacation'. or Notebook JSON is invalid: Non-unique cell id 'medieval-nebraska' detected. Corrected to 'described-commerce'. Notebook JSON is invalid: Non-unique cell id 'meaning-victoria' detected. Corrected to 'occasional-numbers'. Notebook JSON is invalid: Non-unique cell id 'eastern-buyer' detected. Corrected to 'english-benchmark'. Any idea what's going on and how to fix it? Update: It got fixed somehow for a while but then it started doing the same again I still do not get what it was and how it got fixed and ruined again... One fix is to make a copy of the ntb and then discard the old one. Update 9.10.21: It looks that this is no more a problem for python v3.8.11. I have not managed to reproduce the issue since updating all packages so probably the easiest fix now is to update. I have: jupyter core : 4.7.1 jupyter-notebook : 6.4.3 ipython : 7.26.0 ipykernel : 6.2.0
I also happened to find another solution that worked: First select all the cells in the Jupyter Notebook, then press the "cut/scissors" button (don't panic, at this point your Notebook will be empty), and finally press the "paste" button. This is from https://github.com/jupyter/notebook/issues/6001#issuecomment-959828688.
I have the same issue and after reading about it, it seems the cause is copy/pasting cells from other notebooks that were created in other sessions. I haven't found a way to fix it, but at least you can prevent it from happening by copying the content of the cells and not the cells themselves.
Simple Answer STEP 1: Cut all the cells in the Jupyter-notebook using scissor option as shown. STEP 2: Save the notebook now, and paste back the cells cut in the previous step using paste option as shown. STEP 3: Save again and your bug should be fixed now! Hurry, Happy coding.
Code to resave a notebook with the cell name issue resolved: import nbformat as nbf from glob import glob import uuid def get_cell_id(id_length=8): return uuid.uuid4().hex[:id_length] # -- SETUP nb_name = 'my_notebook' # -- MAIN # grab notebook notebooks = list(filter(lambda x: nb_name in x, glob("./*.ipynb", recursive=True))) # iterate over notebooks for ipath in sorted(notebooks): # load notebook ntbk = nbf.read(ipath, nbf.NO_CONVERT) cell_ids = [] for cell in ntbk.cells: cell_ids.append(cell['id']) # reset cell ids if there are duplicates if not len(cell_ids) == len(set(cell_ids)): for cell in ntbk.cells: cell['id'] = get_cell_id() nbf.write(ntbk, ipath) Source: https://github.com/jupyter/notebook/issues/6001#issuecomment-856303981
PowerBI is not passing over the datafield to the Python script
I am trying to run a simple Python script in PowerBI to get it working but it only comes up with many errors.I have tried to: reinstall Numpy and Matplotlib. tried using Python 3.5 and 3.6 Uninstall Conda and reinstall I have a screen shot showing my issue (it all fits on one screen bellow). The data is just a,b,c and 1,2,3 for x and y labeled "Label" and "Data". Screen shot of error and code Code here: # The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: dataset = pandas.DataFrame(Data, Label) # dataset = dataset.drop_duplicates() # Paste or type your script code here: import matplotlib.pyplot as plt dataset.plot(kind='scatter', x='Label', y='Data', color='red') plt.show()
You need to comment out the following: dataset = pandas.DataFrame(Data, Label) Even though it is 'commented out' it will run. It just sets up the data frame ready for the visual, here is a none working example, were it isn't commented out: Hope that helps
How to set the running file path of jupyter in VScode?
When I use the jupyter extension in VScode and run a line of code in jupyter to save a file using relative path,I found the file(iris_tree.dot) in another file. It's just like i debug/run the code in another file path. How can I set the correct path of the jupyter runner? #%% from sklearn.tree import export_graphviz export_graphviz( tree_clf, out_file="iris_tree.dot", feature_names=iris.feature_names[2:], class_names=iris.target_names, rounded=True, filled=True )
Just update the value of "Notebook File Root" to ${workspaceFolder} or ${fileDirname}.
I'm one of the developers on this extension. By default we follow the VSCode pattern of working directory as opposed to the Jupyter pattern. Meaning that we use the root of the currently open workspace folder as the current working directory for starting jupyter notebooks. That might be what is confusing you here. To get around this you can either set cwd in your notebook code as redhatvicky mentioned or you can change the default current working directory in the following VSCode setting. Jupyter -> Notebook File Root Since you can change that setting per workspace you can have it always default to a specific location when working in just the workspace that contains your file. Edit 2/16/22 New setting location
#Ian Huff's answer is still valid, however the setting seems to have changed location since then. Instead of "Python -> Data Science -> Notebook File Root", it's now "Jupyter -> Notebook File Root"
Your question seems quite confusing and I am unable to post a comment. Please follow this link. As per your question, I think the issue is you need to select the correct python interpreter by CTRL+SHIFT+P and then Python: Select Interpreter to select the correct conda environment or a conda interpreter. Otherwise you can try and execute the following code to change your directory before any other command: import os try: os.chdir(os.path.join(os.getcwd(), 'path_to_folder_to_have_the_file')) # '.' if the path is to current folder print(os.getcwd()) except: pass
Generally you can change the working directory by using "os.chdir(NEW_PATH)" One another Suggestion is that , You can set the location to save the image from the code itself. Here below is the code which might help you. from __future__ import division, print_function, unicode_literals # Common imports import numpy as np import os # to make this notebook's output stable across runs np.random.seed(42) # To plot pretty figures import matplotlib.pyplot as plt plt.rcParams['axes.labelsize'] = 14 plt.rcParams['xtick.labelsize'] = 12 plt.rcParams['ytick.labelsize'] = 12 # Where to save the figures PROJECT_ROOT_DIR = "." CHAPTER_ID = "decision_trees" def image_path(fig_id): return os.path.join(PROJECT_ROOT_DIR, "images", CHAPTER_ID, fig_id) def save_fig(fig_id, tight_layout=True): print("Saving figure", fig_id) if tight_layout: plt.tight_layout() print(image_path(fig_id) + ".png") plt.savefig(image_path(fig_id) + ".png", format='png', dpi=300) save_fig("Fig-01-6TFG")
Print Variable In Jupyter Notebook Markdown Cell Python
Can I print the value of a variable in Markdown Cell Jupyter Notebook? Tried Code: value = 5.3 Markdown cell --> Value is {{ value }} I want that the Markdown cell should display the value of variable SCREENSHOT
#nilansh bansal's answer works great for Jupyter Notebooks. Unfortunately, it doesn't work for JupyterLab because the plugin is no longer supported (as is the case for all nbextension plugins). Since JupyterLab gains popularity, I wanted to complement the answers so far because it took me quite some time to find a solution. This is because until now there is no plugin compatible with JupyterLab. I have found the following solution for myself by combining this and this SO answers: from IPython.display import Markdown as md # Instead of setting the cell to Markdown, create Markdown from withnin a code cell! # We can just use python variable replacement syntax to make the text dynamic n = 10 md("The data consists of {} observations. Bla, Bla, ....".format(n)) Alternatively, the last line can be simplified as suggested by #Igor Fobia for Python >3.6: md(f"The data consists of {n} observations. Bla, Bla, ....") This leads to the desired output. However, it has the huge disadvantage that the code cell will still be visible when exporting the NB. This can be solved though: Add a tag to the code cell, i.e. name it "hide" Configure nbconvert to ignore the tagged cells, e.g. by adding this c.TagRemovePreprocessor.remove_input_tags = {"hide"} to your ~/.jupyter/jupyter_notebook_config.py config file I have written a detailed blog-post about how I implemented this solution for publishing Notebooks on my blog. If you use JupyterLab < 2.0, you could install the jupyterlab-celltags plugin for JupyterLab to simplify the cell tagging.
So After going through all the links I was able to resolve the problem by referring to nbextension jupyter notebook docs : https://github.com/ipython-contrib/jupyter_contrib_nbextensions Steps Taken: pip install jupyter_contrib_nbextensions jupyter contrib nbextension install --user jupyter nbextension enable python-markdown/main After the above commands started a jupyter notebook and to print the value of a variable in the markdown cells works like charm! You just have to use {{ ac_score }} within a markdown cell. Screenshot Thanks!
You can overwrite %%markdown IPython magic to substitute variables from the global environment: from IPython.display import Markdown from IPython.core.magic import register_cell_magic #register_cell_magic def markdown(line, cell): return Markdown(cell.format(**globals())) This has the advantage of allowing for Markdown linting when used with JupyterLab-LSP. If developing a documentation with nbsphinx you can hide the input (source of the cell) by setting {"hide_input": true} in cell metadata: (note the Jupyter[Lab] and LSP underlined as those are not in the English dictionary - linting works!) which results in smooth docs like this: For a better experience when in JupyterLab you can always collapse the input cell by clicking on the blue bar to the left of it (visible when you hover over the cell).