# How to write HTML from julia?

**URL:** <https://discourse.julialang.org/t/how-to-write-html-from-julia/62893>\
**Category:** New to Julia\
**Created:** [June 14, 2021, 2:54pm UTC](https://discourse.julialang.org/t/how-to-write-html-from-julia/62893 "2021-06-14T14:54:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tp2750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tp2750/32/207806_2.png) [@tp2750](https://discourse.julialang.org/u/tp2750)\
**Post date:** [June 14, 2021, 2:54pm UTC](https://discourse.julialang.org/t/how-to-write-html-from-julia/62893/1 "2021-06-14T14:54:12Z")

</div>

We have great packages for embedding julia code into markdown documents, and render HTML using [Weave.jl](https://github.com/JunoLab/Weave.jl) or [Literate.jl](https://github.com/fredrikekre/Literate.jl).  
However, sometimes I’ll like to generate an HTML-file from inside a julia-script rather than using one the templating packages. Something similar to what the [hwriter](https://cran.r-project.org/web/packages/hwriter/) R-package does.

The following works, but it is somewhat low-level:

```julia
julia> open("/tmp/index.html", "w") do io
           show(io, "text/html", html"""<!DOCTYPE html><HTML lang = "en"><HEAD><meta charset="UTF-8"/></HEAD><BODY>""")
           show(io, "text/html", html"<h1>My title</h1>")
           show(io, "text/html", html"Some text.")
           show(io, "text/html", html"</BODY></HTML>")
       end

```

Is there already a package to help make this more ergonomic?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 14, 2021, 3:20pm UTC](https://discourse.julialang.org/t/how-to-write-html-from-julia/62893/2 "2021-06-14T15:20:07Z")

</div>

Maybe [GitHub - JuliaPluto/HypertextLiteral.jl: Julia library for the string interpolation of HTML and SVG](https://github.com/MechanicalRabbit/HypertextLiteral.jl)?

---

<div class="post-metadata">

**Author:** ![tp2750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tp2750/32/207806_2.png) [@tp2750](https://discourse.julialang.org/u/tp2750)\
**Post date:** [June 15, 2021, 8:16am UTC](https://discourse.julialang.org/t/how-to-write-html-from-julia/62893/4 "2021-06-15T08:16:37Z")

</div>

Thanks for the link to [HypertextLiteral](https://github.com/MechanicalRabbit/HypertextLiteral.jl) @dpsanders .  
I did not know of that, and it is definitely relevant for this usecase.

It appears to be mainly focused on improving on the `@html_str` string macro from base Julia.

I’m looking for something to reduce the amount of “boiler-plate” I need to type to generate an HTML-document, and as far as I can see, that is not the goal of HypertextLiteral.

---

<div class="post-metadata">

**Author:** ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)\
**Post date:** [June 15, 2021, 8:57am UTC](https://discourse.julialang.org/t/how-to-write-html-from-julia/62893/5 "2021-06-15T08:57:18Z")

</div>

[GitHub - JunoLab/Hiccup.jl: HTML DSL AFAICT](https://github.com/JunoLab/Hiccup.jl) and [GitHub - JuliaWeb/Hyperscript.jl: Hyperscript: A lightweight DOM representation for Julia](https://github.com/JuliaWeb/Hyperscript.jl) come to mind for this if you’re trying to reduce boilerplate. Both work pretty well from what I recall.

---

<div class="post-metadata">

**Author:** ![tp2750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tp2750/32/207806_2.png) [@tp2750](https://discourse.julialang.org/u/tp2750)\
**Post date:** [June 15, 2021, 2:46pm UTC](https://discourse.julialang.org/t/how-to-write-html-from-julia/62893/6 "2021-06-15T14:46:02Z")

</div>

Thanks @mike. Both Hiccup and Hyperscript look like they can help me write HTML in a simple way.  
The READMEs don’t show how to generate a full HTML document and write it to a file, so I’ll take a look at the source, and see if I can generate a simple example.

---

<div class="post-metadata">

**Author:** ![tp2750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tp2750/32/207806_2.png) [@tp2750](https://discourse.julialang.org/u/tp2750)\
**Post date:** [June 17, 2021, 6:17pm UTC](https://discourse.julialang.org/t/how-to-write-html-from-julia/62893/7 "2021-06-17T18:17:04Z")

</div>

Thanks for the solution @mike (even 2 solutions 😃 ).

Here’s a way to do it in [Hyperscript](https://github.com/JuliaWeb/Hyperscript.jl).  
That is a clear improvement over the naive `@html_str` version:

```julia
julia> using Hyperscript

julia> cont =
    [
        m("h1", "My title"),
        "Some text",
    ] ;

julia> doc = 
    [
        m("head",
          m("meta", charset="UTF-8"),
          ),
        m("body",
          cont,
          )
    ]
2-element Vector{Hyperscript.Node{Hyperscript.HTMLSVG}}:
 <head><meta charset="UTF-8" /></head>
 <body><h1>My title</h1>Some text</body>

julia> savehtml("/tmp/hyper.html", doc... ) ;

shell> cat /tmp/hyper.html
<!doctype html>
<html><head><meta charset="UTF-8" /></head><body><h1>My title</h1>Some text</body></html>

```

---

<div class="post-metadata">

**Author:** ![tp2750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tp2750/32/207806_2.png) [@tp2750](https://discourse.julialang.org/u/tp2750)\
**Post date:** [June 17, 2021, 6:44pm UTC](https://discourse.julialang.org/t/how-to-write-html-from-julia/62893/8 "2021-06-17T18:44:53Z")

</div>

[Hiccup](https://github.com/JunoLab/Hiccup.jl) is also nice.  
It does not have a dedicated save method, as far as I can see.

Here’s one way:

```julia
julia> using Hiccup

julia> cont =
    [
        h1("My title"),
        "Some text"
    ] ;

julia> doc = html(
    head(
        meta(charset="UTF-8")
    ),
    body(
        cont
    )
)
<html><head><meta charset="UTF-8" /></head><body><h1>My title</h1>Some text</body></html>

julia> open("/tmp/hiccup.html", "w") do io
    show(io, "text/html", html"<!DOCTYPE html>")
    show(io, "text/html", doc)
end

shell> cat /tmp/hiccup.html
<!DOCTYPE html><html><head><meta charset="UTF-8" /></head><body><h1>My title</h1>Some text</body></html>

```

---

<div class="post-metadata">

**Author:** ![waldyrious](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/waldyrious/32/80_2.png) [@waldyrious](https://discourse.julialang.org/u/waldyrious)\
**Post date:** [June 18, 2021, 3:48pm UTC](https://discourse.julialang.org/t/how-to-write-html-from-julia/62893/9 "2021-06-18T15:48:40Z")

</div>

Thanks for sharing! Have you considered submitting those two examples to the respective READMEs? It would be a great help to others with the same question. 🙂

---

<div class="post-metadata">

**Author:** ![tp2750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tp2750/32/207806_2.png) [@tp2750](https://discourse.julialang.org/u/tp2750)\
**Post date:** [June 18, 2021, 7:14pm UTC](https://discourse.julialang.org/t/how-to-write-html-from-julia/62893/10 "2021-06-18T19:14:57Z")

</div>

Good idea @waldyrious .

I’ll look at this over the weeked.

I actually filed an [issue](https://github.com/JuliaWeb/Hyperscript.jl/issues/37) while preparing the first example 😄

---

<div class="post-metadata">

**Author:** ![tp2750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tp2750/32/207806_2.png) [@tp2750](https://discourse.julialang.org/u/tp2750)\
**Post date:** [June 22, 2021, 7:53pm UTC](https://discourse.julialang.org/t/how-to-write-html-from-julia/62893/11 "2021-06-22T19:53:17Z")

</div>

Done for Hyperscript: [Pull request #38](https://github.com/JuliaWeb/Hyperscript.jl/pull/38). I’ll probably leave it with that.
