Backend Web Services

Example programs

This is all you need to write an application that does post to GPT and returns the result in the textarea. No javascript needed! Only HTML and equal naming of field names. The prompt-id of the html element should match the field name server side.

Server side sourc

class GPT(Component):

    class Children:
        prompt = Component
        answer = Component

        class submit(Component):
            async def trigger(self, id_, event, data):
                print("GOGOG", event, data)
                return await super().trigger(id_, event, data)

            async def on_click(self, data):
                from xmlrpc.client import ServerProxy

                client = ServerProxy("https://api.molodetz.nl/rpc")
                prompt = await self.app.prompt.get_attr("value")
                print(prompt)
                exit(0)

                await self.answer.set_attr("value", client.gpt4o(prompt))

HTML Source

<div>
    <textarea id="prompt" type="text" value=""></textarea>
    <textarea id="answer" type="text" value=""></textarea>
    <input id="submit" type="button" value="Submit" />

</div>

Component using it's own service that frequently repeats.

Every method prefixed with task_ will have it's own thread. Background tasks! In this example, this counter (a text field, defined by HTML) has two background workers. An task_increment that does +1 every second using pure python. Another task that only does something every ten seconds.

Server side source

class Counter(Component):

    async def task_test(self):
        while True:
            await asyncio.sleep(10)
            print("Slow task")

    async def task_increment(self):
        if not self.value:
            self.value = 0
        while True:
            try:
                self.value = int(self.value)
            except:
                self.value = 0
            self.value += 1
            await self.set_attr("value", self.value)
            await asyncio.sleep(1)

HTML page

 <input id="teller1" type="text" value="[some text]" />

Interactive

Here's an example of an interactive calculator that calculates your expression after typing f. No javascript need and no polling. It' a textbox, but if you would change it to a textarea, it doesn't matter. As long if it has the right javascript events.

Eval server side execution

class EvalBox(Component):

    async def on_change(self, value):

        try:
            if value and value.strip().endswith("="):
                value = value.strip()[:-1]
                try:
                    result = eval(value)
                    value = value + "= " + str(result)
                    await self.set_attr("value", value)
                except:
                    pass
        except AttributeError:
            print(value)
        return value

HTML

<input id="eval_box" type="text" value="wiii" />

Random string stream from the Demo

This is the code of that random ascii banner.

class RandomString(Component):

    async def task_random(self):
        import random

        rand_bytes = [random.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(15)]
        random_data = "".join(rand_bytes)
        while True:
            remember = random_data[0]
            random_data = random_data[1:] + remember
            await self.set_attr("innerHTML", random_data)
            await asyncio.sleep(0.01)