k1lib.serve module
This module is for quickly serving Python functions in an interactive website, so that you can build interfaces for your experiments real quick. Let’s say you have a function in the file “a.py”:
def endpoint(a:int=3, b:float=4, c:bool=True) -> float:
if c: return a + b
else: return a * b
You want to be able to expose a nice interactive interface so that you can present to everyone, or to be used in other systems, then you can do something like this:
cbs = k1.Callbacks()
cbs.add(serve.FromPythonFile("a.py"))
cbs.add(serve.BuildPythonFile(port=5138))
cbs.add(serve.StartServer())
cbs.add(serve.GenerateHtml(htmlFile="index.html"))
serve.serve(cbs)
This will start up a local server at the specified port (this case 5138), and dumps a
index.html
file in the current folder. Opening it up will give you this interface:

That’s pretty much it. You can add in your own callbacks, to enable further integration with your systems. You can also customize the given callbacks more.
Currently, these data types are supported, together with their appearance on the interface:
int, float, str: text box
bool: checkbox toggle
bytes,
PIL.Image.Image
: file uploadk1lib.Range
: continuous sliderrange
: stepped sliderlist
: dropdown
See a few demo examples at https://mlexps.com/
- class k1lib.serve.main.FromNotebook(fileName, tagName='serve')[source]
Bases:
Callback
- class k1lib.serve.main.BuildPythonFile(port=None)[source]
Bases:
Callback
- class k1lib.serve.main.StartServer(initTime=10)[source]
Bases:
Callback
- class k1lib.serve.main.GenerateHtml(serverPrefix=None, htmlFile=None, title='Interactive demo')[source]
Bases:
Callback
- __init__(serverPrefix=None, htmlFile=None, title='Interactive demo')[source]
Generates a html file that communicates with the server.
- Parameters
serverPrefix – prefix of server for back and forth requests, like “https://example.com/proj1”. If empty, tries to grab
cbs.l["serverPrefix"]
, which you can deposit from your own callback. If that’s not available then it will fallback tolocalhost:port
htmlFile – path of the target html file. If not specified then a temporary file will be created and made available in
cbs.l["htmlFile"]
title – title of html page
- k1lib.serve.main.commonCbs()[source]
Grabs common callbacks, including
BuildPythonFile
andStartServer