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/多语言笔记.1.4.2.使用 导入命令.ipynb

730 lines
21 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": [
{
"cell_type": "markdown",
"id": "065bfcac",
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"使用导入魔法命令 `#!import`\n",
"========================="
]
},
{
"cell_type": "markdown",
"id": "57caa135",
"metadata": {},
"source": [
"笔记本中中可以管理和使用nuget包非常方便。\n",
"\n",
"但只使用Nuget包远远不够。还可以在不将其复制到笔记本的情况下引用其它笔记、脚本文件(C#、F#、Powershell、Python等语言脚本)、C#编译后的Dll类文件等并且在后续代码中使用引用执行后的结果。\n",
"\n",
"#!import 魔法命令提供了这一功能。运行时,#!import 命令会从指定路径读取文件,并立即执行其中的代码。对于包含多个单元格的导入笔记本文件,这些单元格将按照它们在文件中出现的顺序运行。"
]
},
{
"cell_type": "markdown",
"id": "1d29c29a",
"metadata": {},
"source": [
"## 支持的导入类型"
]
},
{
"cell_type": "markdown",
"id": "3b31f420",
"metadata": {},
"source": [
"由于.NET Interactive支持多种语言#!import魔法命令可以识别多种语言。如果指定的文件是支持多种语言的格式例如.ipynb或.dib那么文件中的不同代码片段将使用其相应的子内核运行只要导入的内核有对应语言的子内核。\n",
"但是#!import不仅仅限于导入其他笔记本文件它还可以直接加载已知语言的源代码文件包括\n",
"| 文件扩展名 | 详情 | \n",
"| ---- | ---- | \n",
"| .cs | 通用C#注意这些将使用C#脚本编译器进行编译。) | \n",
"| .csx | C# 脚本 | \n",
"| .fs | 通用F# 脚本 | \n",
"| .fsx | F# 脚本 | \n",
"| .html | HTML | \n",
"| .js | JavaScript | \n",
"| .ps1 | PowerShell | "
]
},
{
"cell_type": "markdown",
"id": "b9441fa5",
"metadata": {},
"source": [
"## 导入其它笔记文件"
]
},
{
"cell_type": "markdown",
"id": "4966220f",
"metadata": {},
"source": [
"> 真实使用时比如做教程、写项目演示文档等都是有很多文档。有些效果要在多外文档中演示有些脚本、Nuget包、Dll文件等在多个文档中都需要引入;有些需要做成工具类,到处使用等等。\n",
"> 这时,把公共部分提出来,放在单独的文档中编辑好效果,供其它文档引用;这样非常方便。"
]
},
{
"cell_type": "markdown",
"id": "b59e49fd",
"metadata": {},
"source": [
"### 简单导入示例:运行单元格看效果"
]
},
{
"cell_type": "markdown",
"id": "795a8e47",
"metadata": {},
"source": [
"+ 相对路径:相对当前文档目录"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"我是共享单元格,执行输出!\r\n"
]
}
],
"source": [
"#!import shared/doc/Hello.ipynb"
]
},
{
"cell_type": "markdown",
"id": "b54e59da",
"metadata": {},
"source": [
"+ 相对路径:相对当前文档目录"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"我是共享单元格,执行输出!\r\n"
]
}
],
"source": [
"#!import ./shared/doc/Hello.ipynb"
]
},
{
"cell_type": "markdown",
"id": "c90d23c7",
"metadata": {},
"source": [
"+ 绝对路径:因为路径太固定,换个位置或换台电脑,就不能执行。 可使用下面的动态导入"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"我是共享单元格,执行输出!\r\n"
]
}
],
"source": [
"//执行很可能报错,看下面 \"动态导入\"\n",
"#!import \"C:\\Users\\ruyu\\Desktop\\多语言笔记学习\\Docs\\shared\\doc\\Hello.ipynb\""
]
},
{
"cell_type": "markdown",
"id": "af4bb3da",
"metadata": {},
"source": [
"### 动态导入(高级导入)\n",
"使用绝对路径时,可移置性就太差啦!或者 需要导入10个或不确定的多个文件那就很繁琐甚至无法完成。我总结了一个高级的动态用法。"
]
},
{
"cell_type": "markdown",
"id": "a28524d7",
"metadata": {},
"source": [
"+ 示例1利用C#程序,多次执行导入"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "c0d8919d",
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"我是共享单元格,执行输出!\n",
"我是共享单元格,执行输出!\n",
"我是共享单元格,执行输出!\n"
]
}
],
"source": [
"using Microsoft.DotNet.Interactive;\n",
"using Microsoft.DotNet.Interactive.Commands;\n",
"{ //动态执行:多次执行\n",
"\n",
" for(int i=0; i<3; i++)\n",
" {\n",
" await Kernel.Root.SendAsync(new SubmitCode( $\"#!import ./shared/doc/Hello.ipynb\", \"csharp\"));\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "a8f27dff",
"metadata": {},
"source": [
"+ 示例2利用C#程序,灵活设置各种目录"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"我是共享单元格,执行输出!\r\n"
]
}
],
"source": [
"using Microsoft.DotNet.Interactive;\n",
"using Microsoft.DotNet.Interactive.Commands;\n",
"{ //动态执行:绝对路径(当前目录转换为绝对目录,增加可移置性与灵活性)\n",
"\n",
" string currentDic = Environment.CurrentDirectory;\n",
" string fullPath = System.IO.Path.Combine(currentDic,\"shared/doc/Hello.ipynb\");\n",
"\n",
" await Kernel.Root.SendAsync(new SubmitCode( $\"#!import {fullPath}\", \"csharp\"));\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "210c65fd",
"metadata": {},
"source": [
"### 综合应用"
]
},
{
"cell_type": "markdown",
"id": "e75722d9",
"metadata": {},
"source": [
"创建一个 `初始化文档` ini.ipynb, 统一放置公用nuget包引用、公用命名空间引用、公共对象、公用方法与共享变量等供其它文档统一引用进行初始化。"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/markdown": [
"\n",
"[comment]: <公用初始化文档>\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>Newtonsoft.Json, 13.0.3</span></li></ul></div></div>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#!import \"./shared/doc/Ini.ipynb\""
]
},
{
"cell_type": "markdown",
"id": "f0a05dcd",
"metadata": {},
"source": [
"+ 使用导入文档中引用 nuget 包及引用的命名空间"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"Name\":\"张三\",\"Age\":52}\n",
"{\"Name\":\"张三\",\"Age\":52}\n"
]
}
],
"source": [
"//使用 Newtonsoft.Json\n",
"var person = new {Name = \"张三\", Age = 52};\n",
"\n",
"//没有引用命名空间要写全\n",
"var jsonText1 = Newtonsoft.Json.JsonConvert.SerializeObject(person);\n",
"Console.WriteLine(jsonText1);\n",
"\n",
"//初始化文档中引入了之后\n",
"var jsonText2 = JsonConvert.SerializeObject(person);\n",
"\n",
"Console.WriteLine(jsonText2);"
]
},
{
"cell_type": "markdown",
"id": "ffa1029f",
"metadata": {},
"source": [
"+ 使用导入文档中的公用类"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>Submission#34+Student</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Id</td><td><div class=\"dni-plaintext\"><pre>2</pre></div></td></tr><tr><td>Name</td><td><div class=\"dni-plaintext\"><pre>王安全</pre></div></td></tr><tr><td>Age</td><td><div class=\"dni-plaintext\"><pre>12</pre></div></td></tr></tbody></table></div></details><style>\r\n",
".dni-code-hint {\r\n",
" font-style: italic;\r\n",
" overflow: hidden;\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview {\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview td {\r\n",
" vertical-align: top;\r\n",
" text-align: start;\r\n",
"}\r\n",
"details.dni-treeview {\r\n",
" padding-left: 1em;\r\n",
"}\r\n",
"table td {\r\n",
" text-align: start;\r\n",
"}\r\n",
"table tr { \r\n",
" vertical-align: top; \r\n",
" margin: 0em 0px;\r\n",
"}\r\n",
"table tr td pre \r\n",
"{ \r\n",
" vertical-align: top !important; \r\n",
" margin: 0em 0px !important;\r\n",
"} \r\n",
"table th {\r\n",
" text-align: start;\r\n",
"}\r\n",
"</style>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"Student student = new Student(){Id=2, Name = \"王安全\", Age = 12};\n",
"student.Display();"
]
},
{
"cell_type": "markdown",
"id": "5839519f",
"metadata": {},
"source": [
"+ 用导入文档中的公用变量"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>Submission#34+Student</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Id</td><td><div class=\"dni-plaintext\"><pre>1</pre></div></td></tr><tr><td>Name</td><td><div class=\"dni-plaintext\"><pre>班长</pre></div></td></tr><tr><td>Age</td><td><div class=\"dni-plaintext\"><pre>25</pre></div></td></tr></tbody></table></div></details><style>\r\n",
".dni-code-hint {\r\n",
" font-style: italic;\r\n",
" overflow: hidden;\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview {\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview td {\r\n",
" vertical-align: top;\r\n",
" text-align: start;\r\n",
"}\r\n",
"details.dni-treeview {\r\n",
" padding-left: 1em;\r\n",
"}\r\n",
"table td {\r\n",
" text-align: start;\r\n",
"}\r\n",
"table tr { \r\n",
" vertical-align: top; \r\n",
" margin: 0em 0px;\r\n",
"}\r\n",
"table tr td pre \r\n",
"{ \r\n",
" vertical-align: top !important; \r\n",
" margin: 0em 0px !important;\r\n",
"} \r\n",
"table th {\r\n",
" text-align: start;\r\n",
"}\r\n",
"</style>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"SharedStudent.Display();"
]
},
{
"cell_type": "markdown",
"id": "3e67c5fe",
"metadata": {},
"source": [
"+ 使用导入文档中的公用方法(静态方法做工具类)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/plain": [
"学生信息 学号:1, 姓名:班长, 年龄25"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"var s = StudentToString(SharedStudent);\n",
"s.Display();"
]
},
{
"cell_type": "markdown",
"id": "1f919754",
"metadata": {},
"source": [
"## 导入 C# 脚本"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#!import \"./shared/script/tools.cs\""
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/plain": [
"c:\\Users\\ruyu\\Desktop\\多语言笔记学习\\Docs"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"//作用C#脚本功能\n",
"var callTools = Tools.GetCurrentPath();\n",
"callTools.Display();"
]
},
{
"cell_type": "markdown",
"id": "43de23d5",
"metadata": {},
"source": [
"## 导入 PowerShell 脚本"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" Directory: C:\\Users\\ruyu\\Desktop\\多语言笔记学习\\Docs\n",
"\n",
"\u001b[32;1mMode \u001b[0m\u001b[32;1m LastWriteTime\u001b[0m \u001b[32;1;3m Length\u001b[0m\u001b[32;1m Name\u001b[0m\n",
"\u001b[32;1m---- \u001b[0m \u001b[32;1m -------------\u001b[0m \u001b[32;1m ------\u001b[0m \u001b[32;1m----\u001b[0m\n",
"d---- 2025/4/23 11:48 \u001b[44;1m.vscode\u001b[0m\n",
"d---- 2025/4/23 11:55 \u001b[44;1massets\u001b[0m\n",
"d---- 2025/4/23 11:56 \u001b[44;1mshared\u001b[0m\n",
"d---- 2025/4/23 7:31 \u001b[44;1mWebSite\u001b[0m\n",
"-a--- 2025/4/17 16:28 3678 安装与配置-Jupyter Notebook.md\n",
"-a--- 2025/4/17 16:03 2130 安装与配置-Polyglot Notebooks.md\n",
"-a--- 2025/4/23 8:51 747 多语言笔记.0.导航目录.ipynb\n",
"-a--- 2023/5/27 11:42 7998 多语言笔记.1.0.1概述与入门.ipynb\n",
"-a--- 2023/5/27 11:42 3364 多语言笔记.1.0.2常见问题.ipynb\n",
"-a--- 2025/4/20 18:17 10351 多语言笔记.1.1.入门说明.ipynb\n",
"-a--- 2025/4/23 10:11 3493 多语言笔记.1.1.入门说明.md\n",
"-a--- 2025/4/23 9:58 583 多语言笔记.1.2.魔法命令.ipynb\n",
"-a--- 2025/4/21 8:46 9546 多语言笔记.1.2.显示程序执行结果.ipynb\n",
"-a--- 2025/4/17 3:04 55558 多语言笔记.1.2.显示格式化.ipynb\n",
"-a--- 2025/4/17 15:41 9043 多语言笔记.1.2.PocketView渲染html.ipynb\n",
"-a--- 2023/5/27 11:42 3079 多语言笔记.1.3.多语言.ipynb\n",
"-a--- 2025/4/23 10:17 7606 多语言笔记.1.4.1.管理 NuGet包.ipynb\n",
"-a--- 2025/4/23 13:54 11593 多语言笔记.1.4.2.使用 导入命令.ipynb\n",
"-a--- 2025/4/23 9:48 981 多语言笔记.10.1.其它客户端.ipynb\n",
"-a--- 2025/4/20 18:12 2873 多语言笔记.100.1.测试.ipynb\n",
"-a--- 2025/4/23 9:58 83014 多语言笔记.2.1.操作数据库-SQL内核.ipynb\n",
"-a--- 2025/4/18 19:45 60535 多语言笔记.2.2.操作数据库-C#.ipynb\n",
"-a--- 2025/4/21 8:59 327901 多语言笔记.200.工具.ipynb\n",
"-a--- 2025/4/5 12:42 2089 多语言笔记.3.1.交互式 Razor.ipynb\n",
"-a--- 2025/4/5 13:10 13599 多语言笔记.3.2.交互式 Blazor.ipynb\n",
"-a--- 2025/4/23 9:55 1374 多语言笔记.300.技巧与总结.ipynb\n",
"-a--- 2024/5/26 2:54 62771 多语言笔记.4.1.使用 WebApi.ipynb\n",
"-a--- 2024/5/26 2:54 7979 多语言笔记.5.1.使用扩展库.ipynb\n",
"-a--- 2025/4/23 10:11 4516 多语言笔记.6.1.共享变量.ipynb\n",
"-a--- 2025/4/19 21:14 4031 多语言笔记.7.1.运行xUnit单元测试.ipynb\n",
"-a--- 2025/4/19 21:14 4451 多语言笔记.8.1.运行Benchmark基准测试.ipynb\n",
"-a--- 2025/4/23 9:51 11172 多语言笔记.9.1.使用 html.ipynb\n",
"-a--- 2025/4/20 18:19 6435 多语言笔记.9.2.使用 python内核.ipynb\n",
"-a--- 2025/4/23 9:51 981 多语言笔记.9.3.扩展 NET Interactive.ipynb\n",
"-a--- 2025/4/15 18:13 6915 Base.ipynb\n",
"-a--- 2025/4/15 18:44 455 config.json\n",
"\n"
]
}
],
"source": [
"#!import \"./shared/script/tools.ps1\""
]
},
{
"cell_type": "markdown",
"id": "6c4403c1",
"metadata": {},
"source": [
"## 导入 JS 脚本"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"polyglot_notebook": {
"kernelName": "javascript"
}
},
"outputs": [
{
"data": {
"text/plain": [
"我是来自js脚本的信息"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#!import \"./shared/script/tools.js\""
]
},
{
"cell_type": "markdown",
"id": "f4a40f9f",
"metadata": {},
"source": [
"## 导入 html"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<!DOCTYPE html>\r\n",
"<html lang=\"en\">\r\n",
"<head>\r\n",
" <meta charset=\"UTF-8\">\r\n",
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\r\n",
" <title>Html文档</title>\r\n",
"</head>\r\n",
"<body>\r\n",
" <div>\r\n",
" <h1>我是一级标题</h1>\r\n",
" <h2>我是二级标题</h2>\r\n",
" </div>\r\n",
"</body>\r\n",
"</html>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#!import \"./shared/script/tools.html\""
]
},
{
"cell_type": "markdown",
"id": "f7bccfcd",
"metadata": {},
"source": [
"## .NET Projects"
]
},
{
"cell_type": "markdown",
"id": "68d52fc4",
"metadata": {},
"source": [
"目前不支持在.NET项目上下文中导入或引用完整的.NET项目或源文件。如果你在.NET项目中有想要在笔记本中使用的功能你可以先编译它然后使用#r '/path/to/assembly.dll'直接引用程序集,或者将其打包并使用#r nuget引用NuGet包。\n",
"不过,正在考虑改进与.NET项目的集成。"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"languageName": "csharp",
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}