microsoft / vscode-jupyter

VS Code Jupyter extension
https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter
MIT License
1.27k stars 280 forks source link

Code works in Jupyter but not in VS Code #8809

Closed HayesJohnD closed 2 years ago

HayesJohnD commented 2 years ago

Issue Type: Bug

Try to run in VS Code then copy and paste into Jupyter-Lab and try agin. It runs in jupyter-Lab. VS Code tells me the extension variable is not defined on line-16 where I do define it:

for extension[i] in extensions:

extentions is a list with a list within it (one to start with.

extensions = [ [hpp, h] ]

Here is the full source code:

filenames = ["program.c", "studio.hpp", "a.out", "math.hpp", "hpp.out"]
# Generate newfilenames as a list containing the new filenames
# using as many lines of code as your chosen method requires
newfilenames = []
extensions = [["hpp", "h"]]

def new_filenames(filenames):
    count = len(filenames) - 1
    for filename in filenames:
        period = "."
        if period in filename:
            index = filename.find(period)
            partioned = filename.partition(period)
            i = len(extensions) - 1
            for extension[i] in extensions:
                if partioned[2] == extension[i][0]:
                    filename = partioned[0] + partioned[1] + extension[i][1]
                    print("Filename changed: ", filename)

                i += 1
        newfilenames.append(filename)
        count += 1

new_filenames(filenames)
print(filenames)
print(newfilenames)

Output in Jupyter-Lab

Filename changed: studio.h Filename changed: math.h ['program.c', 'studio.hpp', 'a.out', 'math.hpp', 'hpp.out'] ['program.c', 'studio.h', 'a.out', 'math.h', 'hpp.out']

VS Code version: Code 1.63.2 (899d46d82c4c95423fb7e10e68eba52050e30ba3, 2021-12-15T09:39:46.686Z) OS version: Linux x64 5.13.0-27-generic Restricted Mode: No

System Info |Item|Value| |---|---| |CPUs|AMD Ryzen Threadripper 3970X 32-Core Processor (64 x 2200)| |GPU Status|2d_canvas: enabled
gpu_compositing: enabled
multiple_raster_threads: enabled_on
oop_rasterization: disabled_off
opengl: enabled_on
rasterization: disabled_software
skia_renderer: enabled_on
video_decode: disabled_software
vulkan: disabled_off
webgl: enabled
webgl2: enabled| |Load (avg)|1, 1, 1| |Memory (System)|125.65GB (1.39GB free)| |Process Argv|--no-sandbox --unity-launch --crash-reporter-id deb6f0e3-7519-4d91-a772-c67bb3e2b6ee| |Screen Reader|no| |VM|0%| |DESKTOP_SESSION|plasma| |XDG_CURRENT_DESKTOP|KDE| |XDG_SESSION_DESKTOP|KDE| |XDG_SESSION_TYPE|x11|
Extensions (97) Extension|Author (truncated)|Version ---|---|--- rainbow-brackets|2gu|0.0.6 string-converter|ada|0.1.2 armview|ben|0.4.5 vscode-intelephense-client|bme|1.8.0 doxdocgen|csc|1.3.2 python-snippets|cst|0.1.1 vscode-markdownlint|Dav|0.45.0 vscode-eslint|dba|2.2.2 docs-markdown|doc|0.2.100 python-preview|don|0.0.4 vscode-firefox-debug|fir|2.9.6 vscode-textile-preview|Geh|1.3.13 vscode-pull-request-github|Git|0.34.3 go|gol|0.30.0 cloudcode|goo|1.17.0 terraform|has|2.19.0 prettier-eslint-typescript|hb4|1.0.5 vscode-test-explorer|hbe|2.21.1 badges|idl|1.1.0 textile|idl|0.2.0 composer|ika|0.7.1 phpcs|ika|1.0.5 r|Iku|2.3.6 sandbox|jcc|1.0.1 better-cpp-syntax|jef|1.15.10 kite|kit|0.147.0 restructuredtext|lex|168.0.0 vscode-python-test-adapter|lit|0.7.0 rainbow-csv|mec|2.0.0 file-downloader|min|1.0.11 mindaro|min|1.0.120210803 mongodb-vscode|mon|0.8.0 vscode-autoprefixer|mrm|3.0.1 azure-pipelines|ms-|1.195.0 vscode-azureappservice|ms-|0.23.0 vscode-azurefunctions|ms-|1.6.0 vscode-azureresourcegroups|ms-|0.4.0 vscode-azurestorage|ms-|0.12.1 vscode-azurevirtualmachines|ms-|0.4.1 vscode-bicep|ms-|0.4.1124 vscode-cosmosdb|ms-|0.18.1 vscode-docker|ms-|1.19.0 csharp|ms-|1.24.0 vscode-dotnet-runtime|ms-|1.5.0 vscode-edge-devtools|ms-|1.4.2 vscode-kubernetes-tools|ms-|1.3.6 data-workspace-vscode|ms-|0.1.1 mssql|ms-|1.12.0 sql-database-projects-vscode|ms-|0.14.1 python|ms-|2021.12.1559732655 vscode-pylance|ms-|2022.1.3 jupyter|ms-|2021.11.1001550889 jupyter-keymap|ms-|1.0.0 jupyter-renderers|ms-|1.0.4 vscode-ai|ms-|0.6.27 vscode-ai-remote|ms-|0.6.0 remote-containers|ms-|0.209.6 remote-ssh|ms-|0.70.0 remote-ssh-edit|ms-|0.70.0 remote-wsl|ms-|0.63.13 vscode-remote-extensionpack|ms-|0.21.0 azure-account|ms-|0.9.11 azurecli|ms-|0.5.0 cmake-tools|ms-|1.9.2 cpptools|ms-|1.7.1 cpptools-extension-pack|ms-|1.1.0 js-debug-nightly|ms-|2022.1.2317 mono-debug|ms-|0.16.2 test-adapter-converter|ms-|0.1.5 vscode-node-azure-pack|ms-|0.2.1 vscode-typescript-next|ms-|4.6.20220124 vsliveshare|ms-|1.0.5273 vsliveshare-audio|ms-|0.1.91 azurerm-vscode-tools|msa|0.15.5 debugger-for-chrome|msj|4.13.0 debugger-for-edge|msj|1.0.15 vscode-react-native|msj|1.9.0 autodocstring|njp|0.5.4 vetur|oct|0.35.0 indent-rainbow|ode|8.2.2 r-debugger|RDe|0.4.7 ruby|reb|0.28.1 vscode-yaml|red|1.3.0 python-template-snippets|Ric|1.2.0 shebang-snippets|rpi|0.1.4 snyk-vulnerability-scanner|sny|1.2.7 vscode-vuln-cost|sny|1.7.1 rewrap|stk|1.16.0 umlet|the|1.0.4 html-preview-vscode|tht|0.2.5 cmake|twx|0.0.17 msft-todo-unofficial|tyl|0.0.15 vscodeintellicode|Vis|1.2.16 jinja|who|0.0.8 vscode-ruby|win|0.28.0 JavaScriptSnippets|xab|1.8.0 php-debug|xde|1.23.0 (1 theme extensions excluded)
A/B Experiments ``` vsliv368:30146709 vsreu685:30147344 python383cf:30185419 vspor879:30202332 vspor708:30202333 vspor363:30204092 pythonvspyl392cf:30425750 pythontb:30283811 pythonptprofiler:30281270 vsdfh931cf:30280410 vshan820:30294714 vstes263:30335439 pythondataviewer:30285071 vscod805:30301674 pythonvspyt200:30340761 binariesv615:30325510 bridge0708:30335490 bridge0723:30353136 vsaa593cf:30376535 vsc1dsc:30424895 pythonvs932:30410667 wslgetstartedc:30426334 vscop804cf:30404767 vscop940:30404999 vsclayoutctrt:30425946 vsrem710cf:30416617 vsbas813:30426126 vsic-wlc-tsjs:30426436 ```
rchiodo commented 2 years ago

This doesn't work for me in jupyter either. I get this in jupyter?

image

The syntax you have on line 16 is unfamiliar to me. Is that 2.7 syntax? That might explain why this works on your jupyter (assuming your kernel is 2.7)

rchiodo commented 2 years ago

This code seems to do what you showed as the expected output:

filenames = ["program.c", "studio.hpp", "a.out", "math.hpp", "hpp.out"]
# Generate newfilenames as a list containing the new filenames
# using as many lines of code as your chosen method requires
newfilenames = []
extensions = [["hpp", "h"]]

def new_filenames(filenames):
    count = len(filenames) - 1
    for filename in filenames:
        period = "."
        if period in filename:
            index = filename.find(period)
            partioned = filename.partition(period)
            for extension_list in extensions:
                if partioned[2] == extension_list[0]:
                    filename = partioned[0] + partioned[1] + extension_list[1]
                    print("Filename changed: ", filename)
        newfilenames.append(filename)
        count += 1

new_filenames(filenames)
print(filenames)
print(newfilenames)
rchiodo commented 2 years ago

This also works:

extension = [0] # Variable has to be defined here
filenames = ["program.c", "studio.hpp", "a.out", "math.hpp", "hpp.out"]
# Generate newfilenames as a list containing the new filenames
# using as many lines of code as your chosen method requires
newfilenames = []
extensions = [["hpp", "h"]]

def new_filenames(filenames):
    count = len(filenames) - 1
    for filename in filenames:
        period = "."
        if period in filename:
            index = filename.find(period)
            partioned = filename.partition(period)
            i = len(extensions) - 1
            for extension[i] in extensions:
                if partioned[2] == extension[i][0]:
                    filename = partioned[0] + partioned[1] + extension[i][1]
                    print("Filename changed: ", filename)

                i += 1
        newfilenames.append(filename)
        count += 1

new_filenames(filenames)
print(filenames)
print(newfilenames)

Perhaps when you ran this in juptyer you had already defined extension?