You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PolyglotNotebooksStudy/Docs/多语言笔记.9.2.使用python内核.ipynb

253 lines
6.0 KiB
Plaintext

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"在 Polyglot Netebooks 中添加Jupyter内核支持Python语言\n",
"=======================================================\n",
"\n",
"+ 前提系统要安装Python环境推荐使用Anaconda安装直接安装Python也可以\n",
"\n",
"+ 作用:使多语言笔记,直接支持 Python语言\n",
"\n",
"+ 优势在一个笔记文档中可以同时混合多种语言Python、C#、F#、PowerShell、JS、SQL、HTML等"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 安装 Python\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 直接安装:[Python官网](https://www.python.org/) 下载安装即可;\n",
"+ Anaconda[Anaconda官网](https://www.anaconda.com/) 安装后要设置好默认环境;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"``` cmd\n",
" # 保证:在命令行窗口,能直接执行下面命令\n",
" python --version\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 方式一:直接使用魔法命令`#!connect jupyter`\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"直接使用魔术命令: \n",
"\n",
"+ 无Conda环境`#!connect jupyter --kernel-name pythonkernel --kernel-spec python3`\n",
"+ 有Conda环境`#!connect jupyter --kernel-name pythonkernel --conda-env base --kernel-spec python3`\n",
"\n",
"实测有缺点:\n",
"+ 不能重复执行:重复执行会报错\n",
"+ 连接字符串必须是真实字符串:不能是变量,不灵活;明文\"不安全\",比如演示环境\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/markdown": [
"The `#!connect jupyter` feature is in preview. Please report any feedback or issues at https://github.com/dotnet/interactive/issues/new/choose."
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"Kernel added: #!pythonkernel"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"//命名空间\n",
"using Microsoft.DotNet.Interactive;\n",
"using Microsoft.DotNet.Interactive.Commands;\n",
"\n",
"//引入Python内核\n",
"#!connect jupyter --kernel-name pythonkernel --kernel-spec python3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 方式二变通用法在C#程序中,拼接好魔术命令`#!connect`,再发送给内核执行\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"此种方式是魔法命令的变型、优化版本质原理一样只是调用方式换成C#。\n",
"优点:\n",
"+ 多次执行,不报错;\n",
"+ 魔法命令:可自由拼接(可使用配置文件、系统变量、共享变量等),非常灵活;"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"using Microsoft.DotNet.Interactive;\n",
"using Microsoft.DotNet.Interactive.Commands;\n",
"\n",
"//引入Python内核使用C# 执行引入语句\n",
"if(Kernel.Root.FindKernelByName(\"pythonkernel\") == null)\n",
"{\n",
" //Console.WriteLine(\"正在导入Python内核....\");\n",
" var importPythonkernel = \"#!connect jupyter --kernel-name pythonkernel --kernel-spec python3\";\n",
" await Kernel.Root.SendAsync(new SubmitCode( importPythonkernel, \"csharp\"));\n",
" //Console.WriteLine(\"Python内核导入完成\");\n",
"}\n",
"else \n",
"{\n",
" //Console.WriteLine(\"Python内核已经导入\");\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 使用Python语言内核"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 使用 #!pythonkernel 命令切换到Python语言内核\n",
"+ 单元格右下角,选择 `pythonkernel`内核 "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"polyglot_notebook": {
"kernelName": "pythonkernel"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"你好,我是 Ployglot Notebooks 使用 Python语言内核打印的内容\n"
]
}
],
"source": [
"# 明确指定内核:优先级高于 \"单元格选择的内核\"\n",
"#!pythonkernel\n",
"\n",
"print(\"你好,我是 Ployglot Notebooks 使用 Python语言内核打印的内容\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"polyglot_notebook": {
"kernelName": "pythonkernel"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"右下方选择Python内核\n"
]
}
],
"source": [
"# 右下方选择 内核\n",
"print(\"右下方选择Python内核\");"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"polyglot_notebook": {
"kernelName": "pythonkernel"
}
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"name": "polyglot-notebook"
},
"orig_nbformat": 4,
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"name": "csharp"
},
{
"aliases": [],
"languageName": "python",
"name": "pythonkernel"
},
{
"aliases": [],
"name": "razor"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}