Skip to content

Commit

Permalink
Add initial jupyter commands
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerLeonhardt authored and colombod committed Feb 22, 2020
1 parent 9d95180 commit ea6260e
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 1 deletion.
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));
}
}
}
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);
}
}
}
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ NestedModules = @('Microsoft.DotNet.Interactive.PowerShell.dll')
FunctionsToExport = @()

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @('Trace-PipelineObject')
CmdletsToExport = @(
'Get-JupyterHtml',
'Invoke-JupyterJavaScript',
'Show-JupyterContent',
'Trace-PipelineObject')

# Variables to export from this module
VariablesToExport = '*'
Expand Down
190 changes: 190 additions & 0 deletions NotebookExamples/powershell/Docs/HTML.ipynb
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
}

0 comments on commit ea6260e

Please sign in to comment.