forked from dotnet/interactive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d95180
commit ea6260e
Showing
5 changed files
with
306 additions
and
1 deletion.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
Microsoft.DotNet.Interactive.PowerShell/Commands/GetJupyterHtmlCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
using static Microsoft.DotNet.Interactive.Kernel; | ||
|
||
namespace Microsoft.DotNet.Interactive.PowerShell.Commands | ||
{ | ||
using System.Management.Automation; | ||
|
||
/// <summary> | ||
/// Takes the the string input and turns it into an IHtmlContent that can be passed in to | ||
/// Show-JupyterContent to render Html in a Jupyter cell's output. | ||
/// </summary> | ||
[Cmdlet(VerbsCommon.Get, "JupyterHtml")] | ||
[OutputType("AspNetCore.Html.IHtmlContent")] | ||
public sealed class GetJupyterHtmlCommand : PSCmdlet | ||
{ | ||
/// <summary> | ||
/// The object from pipeline. | ||
/// </summary> | ||
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)] | ||
public string HtmlString { get; set; } | ||
|
||
/// <summary> | ||
/// ProcessRecord override. | ||
/// </summary> | ||
protected override void ProcessRecord() | ||
{ | ||
WriteObject(HTML(HtmlString)); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Microsoft.DotNet.Interactive.PowerShell/Commands/InvokeJupyterJavaScriptCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
using static Microsoft.DotNet.Interactive.Kernel; | ||
|
||
namespace Microsoft.DotNet.Interactive.PowerShell.Commands | ||
{ | ||
using System.Management.Automation; | ||
|
||
/// <summary> | ||
/// Takes the the JavaScript string input and invokes it on the client. | ||
/// </summary> | ||
[Cmdlet(VerbsLifecycle.Invoke, "JupyterJavaScript")] | ||
public sealed class InvokeJupyterJavaScriptCommand : PSCmdlet | ||
{ | ||
/// <summary> | ||
/// The JavaScript string to invoke. | ||
/// </summary> | ||
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)] | ||
public string JavaScriptString { get; set; } | ||
|
||
/// <summary> | ||
/// ProcessRecord override. | ||
/// </summary> | ||
protected override void ProcessRecord() | ||
{ | ||
Javascript(JavaScriptString); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
Microsoft.DotNet.Interactive.PowerShell/Commands/ShowJupyterContentCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
using static Microsoft.DotNet.Interactive.Kernel; | ||
|
||
namespace Microsoft.DotNet.Interactive.PowerShell.Commands | ||
{ | ||
using System.Management.Automation; | ||
using Microsoft.DotNet.Interactive.Events; | ||
|
||
/// <summary> | ||
/// Takes the the input and displays it on the client using .NET Interactive's formatters. | ||
/// This returns a DisplayedValue that can then be updated by calling `Update` on the object. | ||
/// </summary> | ||
[Cmdlet(VerbsCommon.Show, "JupyterContent")] | ||
[OutputType("Interactive.Events.DisplayedValue")] | ||
public sealed class ShowJupyterContentCommand : PSCmdlet | ||
{ | ||
/// <summary> | ||
/// The object from pipeline. | ||
/// </summary> | ||
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)] | ||
[AllowNull] | ||
public object InputObject { get; set; } | ||
|
||
/// <summary> | ||
/// The MimeType to use. | ||
/// </summary> | ||
[Parameter(Position = 1)] | ||
public string MimeType { get; set; } | ||
|
||
/// <summary> | ||
/// Determines whether the DisplayedValue should get written to the pipeline. | ||
/// </summary> | ||
[Parameter()] | ||
public SwitchParameter PassThru { get; set; } | ||
|
||
/// <summary> | ||
/// ProcessRecord override. | ||
/// </summary> | ||
protected override void ProcessRecord() | ||
{ | ||
object obj = InputObject is PSObject psObject ? psObject.BaseObject : InputObject; | ||
DisplayedValue displayedValue = display(obj, MimeType); | ||
|
||
if (PassThru.IsPresent) | ||
{ | ||
WriteObject(displayedValue); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Outputting HTML in a PowerShell notebook <img src=\"https://raw.githubusercontent.com/PowerShell/PowerShell/master/assets/Powershell_black_64.png\" align=\"right\"/>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Display Helpers\n", | ||
"\n", | ||
"There are a number of helper methods for writing HTML that are available by default in a .NET notebook." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### HTML" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"If you want to write out a `string` as HTML, you can use the `HTML` method:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"Get-JupyterHtml '<b style=\"color:blue\">Hello!</b>' | Show-JupyterContent" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Displaying HTML using a `string` directly will display the actual string rather than rendering it as HTML. " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"Show-JupyterContent '<b style=\"color:blue\">Hello!</b>'" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The `HTML` method signals that the content is HTML because its return type, `HtmlString`, implements `IHtmlContent`:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"$someHtml = Get-JupyterHtml '<b style=\"color:blue\">Hello!</b>'\n", | ||
"\n", | ||
"Show-JupyterContent $someHtml.GetType()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Javascript" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"You may also want to output JavaScript. You can do this using the `Javascript` helper." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"Invoke-JupyterJavaScript \"alert('Hello!');\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Magic Commands\n", | ||
"\n", | ||
"There are also several magic commands that can be used to output HTML in your .NET notebook.\n", | ||
"\n", | ||
"You can output HTML..." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#!html\n", | ||
"\n", | ||
"<b>Hello!</b>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"...or run JavaScript..." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#!javascript\n", | ||
"\n", | ||
"alert(\"hello\");" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"...or render Markdown." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#!markdown\n", | ||
"\n", | ||
"Write a **list** ...\n", | ||
"* first\n", | ||
"* second\n", | ||
"\n", | ||
"...or a _table_...\n", | ||
"\n", | ||
"|Fruit |Texture |\n", | ||
"|---------|--------|\n", | ||
"|apple |smooth |\n", | ||
"|durian |bumpy |" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": ".NET (PowerShell)", | ||
"language": "PowerShell", | ||
"name": ".net-powershell" | ||
}, | ||
"language_info": { | ||
"file_extension": ".ps1", | ||
"mimetype": "text/x-powershell", | ||
"name": "PowerShell", | ||
"pygments_lexer": "powershell", | ||
"version": "7.0" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |