easyweb

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

README

easyweb

like streamlit, can quickly generate a web

how work

  1. Server and client interact through websocket
  2. The elements of the page are dynamically generated by jquery
  3. The user opens the page, and the page connects to the server through websocket
  4. The server executes the code, dynamically creates elements, and sends them to the client
  5. The elements(First-level) have a unique id, through which to create, update, delete
  6. websocket + jquery(3.7.0 local) + bootstrap(5.3.0 cdn)

exampel

package main

import (
    "fmt"
    "net/http"
    "time"

    "github.com/lengzhao/easyweb"
    "github.com/lengzhao/easyweb/e"
)

func main() {
    easyweb.RegisterPage(func(page easyweb.Page) {
        page.Title("MyWeb")
        page.Write("this is my first ui.")
    }, easyweb.DefaultPagePath...)
    http.ListenAndServe(":8080", nil)
}

The browser opens http://localhost:8080

For more examples, please see the example folder

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultPagePath []string = []string{
	"", "/", "/index.html",
}
View Source
var DefaultPageTmpl string

Functions

func RegisterPage

func RegisterPage(pn PageFunc, path ...string) string

RegisterPage registers a page function to handle HTTP requests for the specified path patterns.

Parameters: - pn: The page function to register. - path: Additional path patterns to register the page function for. if set to "", the caller's filename will be used. e.g. "index.go"-> "index.html"

Return: - string: The WebSocket URL path for the registered page.

func RegisterPageWithID added in v0.1.4

func RegisterPageWithID(pid string, pn PageFunc, path ...string) string

Types

type CbDataType

type CbDataType byte
const (
	CbDataTypeString CbDataType = iota
	CbDataTypeBinary
)

type IAfterLoaded added in v0.1.2

type IAfterLoaded interface {
	AfterElementLoadedFromFramwork(p Page)
}

type IContainerID added in v0.1.7

type IContainerID interface {
	ContainerID() string
}

type IMessageCb

type IMessageCb interface {
	MessageCallbackFromFramwork(page Page, id string, dataType CbDataType, data []byte) bool
}

type Page

type Page interface {
	Title(string) Page
	AddJs(string) Page
	AddCss(string) Page
	RunJs(js string) Page
	Write(any) string
	WriteWithID(string, any) string
	Delete(string)
	SetAttr(id, key, value string) string
	GetPeer() string
	Close()
	WaitUntilClosed()
	// regist element event after loaded
	RegistEvent(id, typ string, cb IMessageCb)

	SetEnv(key string, value any)
	GetEnv(key string) any
	WatchEnv(key string, cb func(value any)) error
}

type PageFunc

type PageFunc func(page Page)

Directories

Path Synopsis
e
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL