Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a local HTML file using Go and webview?

Tags:

html

go

webview

I want to build an application using Go with a GUI baked in. A workable solution for that seems to be webview.

What I have now is this (and it works!):

package main

import (
    "github.com/webview/webview"
)

var count int = 0

func main() {
    w := webview.New(true)
    defer w.Destroy()

    w.SetSize(600, 200, webview.HintNone)

    w.Bind("btn", func() int {
        count++
        return count
    })

    //Create UI with data URI
    w.Navigate(`data:text/html,
    <!doctype html>
    <html>
    ...
    </html>`)

    w.Run()
}

What I would like to do is to build the gui in a seperate workspace so that I have working syntax hilighting, intellisense etc. so something like:

w.Navigate("./GUI/gui.html")

However, I can't get it to work. is it possible anyway?

like image 340
Petit Bateau Avatar asked Oct 11 '25 16:10

Petit Bateau


1 Answers

Update: use file:///

Using the full path to the file.

w.Navigate("file:////Users/myuser/tempgo/hi.html")

Or read the file into memory

Use ioutil.ReadFile to read in the data from an HTML file. This can then be converted to a string.

file, _ := ioutil.ReadFile("hi.html")
stringFile := string(file)
w.Navigate(`data:text/html,` + stringFile)

Full working example

Original code from the question.

package main

import (
    "github.com/webview/webview"
    "io/ioutil"
)

var count int = 0

func main() {
    w := webview.New(true)
    defer w.Destroy()

    w.SetSize(600, 200, webview.HintNone)

    w.Bind("btn", func() int {
        count++
        return count
    })

    file, _ := ioutil.ReadFile("hi.html")

    stringFile := string(file)

    //Create UI with data URI
    w.Navigate(`data:text/html,` + stringFile)

    w.Run()
}

Directory structure:

main.go
hi.html
like image 185
Christian Avatar answered Oct 14 '25 05:10

Christian