# 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
```python
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
```html
```
## 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
```python
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
```
```
## 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
```python
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
```html
```
## Random string stream from the Demo
This is the code of that random ascii banner.
```python
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)
```