Python/Jupyter笔记本未正确突出显示fstring

2024-10-03 17:14:45 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是Jupyter笔记本和2个Python代码片段的示例:

enter image description here

在Jupyter中,只有当它是一个变量时,它大多数时候都不会高亮显示f字符串,即使它被定义并用作a+b,它也不会高亮显示

在Python中,如果定义了一个变量,它将突出显示该变量,但如果它的值包含任何数学运算,则它只是不一致

我试着检查我的settings.json,但不认为问题出在它上面:

{
  "gitlens.advanced.messages": {
    "suppressShowKeyBindingsNotice": true
  },
  "workbench.iconTheme": "Monokai Pro (Filter Spectrum) Icons",
  "liveServer.settings.donotShowInfoMsg": true,
  "explorer.confirmDelete": false,
  "editor.tabSize": 2,
  "editor.rulers": [110],
  "workbench.startupEditor": "none",
  "editor.snippetSuggestions": "top",
  "workbench.statusBar.visible": true,
  "editor.suggestSelection": "first",
  "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
  "python.pythonPath": "/opt/anaconda3/envs/abc/bin/python",
  "workbench.colorCustomizations": {
    "editorRuler.foreground": "#e1b525"
},
"python.autoComplete.addBrackets": true,
  "editor.wordWrapColumn": 200,
  "python.formatting.provider": "black",
  "launch": {
    "configurations": [],
    "compounds": []
  },
  "workbench.colorTheme": "One Dark Pro",
  "python.linting.mypyEnabled": true,
  "python.linting.pylintEnabled": true,
  "python.linting.enabled": false,
  "html.format.enable": false,
  "javascript.format.enable": false,
  "typescript.format.enable": false,
  "json.format.enable": false,
  "python.languageServer": "Pylance",
  "python.analysis.completeFunctionParens": true,
"kite.showWelcomeNotificationOnStartup": false,
"jupyter.askForKernelRestart": false,
"jupyter.textOutputLimit": 0,
"jupyter.alwaysTrustNotebooks": true,
"jupyter.sendSelectionToInteractiveWindow": true,
"workbench.editorAssociations": {
  "*.ipynb": "jupyter.notebook.ipynb"
},
"python.defaultInterpreterPath": "/opt/anaconda3/envs/abc/bin/python",
"[python]": {

  "gitlens.codeLens.symbolScopes": [
    "!Module"
  ],
  "editor.wordBasedSuggestions": false
}
}

也许这部分"python.formatting.provider": "black"会有所不同,但据我所知,它只是格式化,而不是突出显示


更新

My version of Notebook is 6.4.4 but I still have that bad highlight problem.


Tags: jsonfalsetrueformatsettings定义enablejupyter
1条回答
网友
1楼 · 发布于 2024-10-03 17:14:45

升级笔记本的版本
此Github链接(如下)显示您可能需要将笔记本升级到4.4.1或5.0甚至更高版本。
F-string Highlight Problem


  • 升级命令

使用此命令升级:pip install notebook upgrade

Note: Anaconda users may prefer conda update notebook


VS代码之外的Jupyter笔记本电脑
我试着用我的Jupyter笔记本,效果很好。

这就是Jupyter笔记本的外观👇👇

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Behdad Moghadam! :)\n"
     ]
    }
   ],
   "source": [
    "behdad = \"Behdad Moghadam\"\n",
    "print(f\"Hello {behdad}! :)\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}


VS代码中的Jupyter Notbook

我在VS代码中为Jupyter笔记本使用此扩展👇👇

Jupyter Notebook VS-code extension marketplace

  • 结果
    这对我来说很好。

卸载并重新安装Jupyter笔记本电脑

这会将设置和配置重置为正常状态

相关问题 更多 >