-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
with_terminal
and update Pluto to 0.18.4 (#88)
- Loading branch information
1 parent
578e179
commit 8fd732f
Showing
9 changed files
with
190 additions
and
8 deletions.
There are no files selected for viewing
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
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,87 @@ | ||
# `with_terminal` | ||
|
||
`PlutoUI` has a well-known function `with_terminal` to show terminal output with a black background and colored text. | ||
For example, when having loaded `PlutoUI` via `using PlutoUI`, the following code will show the text "Some terminal output" in a mini terminal window inside `Pluto`: | ||
|
||
```julia | ||
with_terminal() do | ||
println("Some terminal output") | ||
end | ||
``` | ||
|
||
This functionality is supported by `PlutoStaticHTML` too. | ||
To make it work, `PlutoStaticHTML` takes the output from `Pluto`, which looks roughly as follows: | ||
|
||
```html | ||
<div style="display: inline; white-space: normal;"> | ||
<script type="text/javascript" id="plutouiterminal"> | ||
let txt = "Some terminal output" | ||
... | ||
</script> | ||
</div> | ||
``` | ||
|
||
and changes it to: | ||
|
||
```html | ||
<pre id="plutouiterminal"> | ||
Some terminal output | ||
</pre> | ||
``` | ||
|
||
This output is now much simpler to style to your liking. | ||
Below, there is an example style that you can apply which will style the terminal output just like it would in `Pluto`. | ||
|
||
In terminals, the colors are enabled via so called ANSI escape codes. | ||
These ANSI colors can be shown correctly by adding the following Javascript to the footer of your website. | ||
This code will loop through all the HTML elements with `id="plutouiterminal"` and apply the `ansi_to_html` function to the body of those elements: | ||
|
||
```html | ||
<script type="text/javascript"> | ||
async function color_ansi() { | ||
const terminalOutputs = document.querySelectorAll("[id=plutouiterminal]"); | ||
// Avoid loading AnsiUp if there is no terminal output on the page. | ||
if (terminalOutputs.length == 0) { | ||
return | ||
}; | ||
try { | ||
const { default: AnsiUp } = await import("https://cdn.jsdelivr.net/gh/JuliaPluto/ansi_up@v5.1.0-es6/ansi_up.js"); | ||
const ansiUp = new AnsiUp(); | ||
// Indexed loop is needed here, the array iterator doesn't work for some reason. | ||
for (let i = 0; i < terminalOutputs.length; ++i) { | ||
const terminalOutput = terminalOutputs[i]; | ||
const txt = terminalOutput.innerHTML; | ||
terminalOutput.innerHTML = ansiUp.ansi_to_html(txt); | ||
}; | ||
} catch(e) { | ||
console.error("Failed to import/call ansiup!", e); | ||
}; | ||
}; | ||
color_ansi(); | ||
</script> | ||
``` | ||
|
||
Next, the output can be made more to look like an embedded terminal by adding the following to your CSS: | ||
|
||
```css | ||
#plutouiterminal { | ||
max-height: 300px; | ||
overflow: auto; | ||
white-space: pre; | ||
color: white; | ||
background-color: black; | ||
border-radius: 3px; | ||
margin-top: 8px; | ||
margin-bottom: 8px; | ||
padding: 15px; | ||
display: block; | ||
font-size: 14px; | ||
} | ||
``` | ||
|
||
!!! note | ||
Note that the Javascript code above downloads the `ansi_up.js` file from a content delivery network (CDN). | ||
This is not advised because CDNs are bad for privacy, may go offline and are bad for performance. | ||
They are bad for performance because browsers do not cache CDN downloaded content between different domains for security reasons. | ||
Therefore, CDN content will cause at least an extra DNS lookup in most cases. |
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
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,21 @@ | ||
function _extract_txt(body) | ||
sep = '\n' | ||
lines = split(body, sep) | ||
index = findfirst(contains("let txt = "), lines) | ||
txt_line = strip(lines[index]) | ||
txt = txt_line[12:end-1] | ||
with_newlines = replace(txt, "\\n" => '\n') | ||
without_extraneous_newlines = strip(with_newlines, '\n') | ||
return without_extraneous_newlines | ||
end | ||
|
||
function _patch_with_terminal(body::String) | ||
txt = _extract_txt(body) | ||
@show txt | ||
return """ | ||
<pre id="plutouiterminal"> | ||
$txt | ||
</pre> | ||
""" | ||
end | ||
precompile(_patch_with_terminal, (String,)) |
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
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,48 @@ | ||
# Body for `cell.output.body` of | ||
# using PlutoUI | ||
# f(x) = Base.inferencebarrier(x); | ||
# with_terminal() do | ||
# @code_warntype f(1) | ||
# end | ||
body = raw""" | ||
<div style="display: inline; white-space: normal;"> | ||
<script type="text/javascript" id="plutouiterminal"> | ||
let txt = "MethodInstance for Main.workspace#4.f(::Int64)\n from f(x) in Main.workspace#4 at /home/rik/git/blog/posts/notebooks/inference.jl#==#9dbfb7d5-7035-4ea2-a6c0-efa00e39e90f:1\nArguments\n #self#[36m::Core.Const(Main.workspace#4.f)[39m\n x[36m::Int64[39m\nBody[91m[1m::Any[22m[39m\n[90m1 ─[39m %1 = Base.getproperty(Main.workspace#4.Base, :inferencebarrier)[36m::Core.Const(Base.inferencebarrier)[39m\n[90m│ [39m %2 = (%1)(x)[91m[1m::Any[22m[39m\n[90m└──[39m return %2\n\n" | ||
var container = html` | ||
<pre | ||
class="PlutoUI_terminal" | ||
style=" | ||
max-height: 300px; | ||
overflow: auto; | ||
white-space: pre; | ||
color: white; | ||
background-color: black; | ||
border-radius: 3px; | ||
margin-top: 8px; | ||
margin-bottom: 8px; | ||
padding: 15px; | ||
display: block; | ||
" | ||
></pre> | ||
` | ||
try { | ||
const { default: AnsiUp } = await import("https://cdn.jsdelivr.net/gh/JuliaPluto/ansi_up@v5.1.0-es6/ansi_up.js"); | ||
container.innerHTML = new AnsiUp().ansi_to_html(txt); | ||
} catch(e) { | ||
console.error("Failed to import/call ansiup!", e) | ||
container.innerText = txt | ||
} | ||
return container | ||
</script> | ||
</div> | ||
""" | ||
|
||
txt = strip(PlutoStaticHTML._extract_txt(body)) | ||
@test startswith(txt, "MethodInstance") | ||
@test endswith(txt, "return %2") | ||
@test contains(txt, '\n') | ||
|
||
patched = PlutoStaticHTML._patch_with_terminal(body) | ||
@test contains(patched, """<pre id="plutouiterminal">""") |
8fd732f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
8fd732f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/56897
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: