Live data with Pixlet/Tidbyt

I’ve built a small pixlet application that fetches some data from an API and displays it. This data changes every few minutes and I would like the tidbyt to update when that happens. Is that possible with pixlet?

Another use case is the clock pixlet example - it shows the current time and then never changes again. How do you push that update every minute to the tidbyt?

I read in another thread that we could run pixlet on a server and re-generate the image and push that image via the API to a tidbyt. Are there any examples for this sort of thing? And can I test out the tidbyt API before I receive my tidbyt?

Any advice/documentation/etc here is greatly appreciated!

Hi btj

Please refer to this thread where I answered a similar question

https://discuss.tidbyt.com/t/clarity-on-sdk-limitations/

Essentially you can run pixlet in a way that is serving a new webp render each time you access it (localhost:port when you are testing)
This allows it to cache api data as seen in the bitcoin tracker example.

I wrote some Mac OS shell script that will curl the local hosted Pixlet server and then capture the webp image using grep.
I then use a web request to send this to the tidbyt api.

Let me know if this could be useful and I can share it.

In the future, Tidbyt plans to host custom apps themselves so this process isn’t necessary, but will be useful for super customized use cases.

Let me know if this could be useful and I can share it.

Sure, I’d like to see it and sounds like it will work when developing locally.

My idea is to have something fully hosted that is running pixlet and hits an API regularly and then updates a tidbyt. Ideally this would have a web interface so tidbyt owners can self-serve sign up to use the app. Could be cool to show the pixlet output as well so folks without a tidbyt could use it via a browser.

Has anyone built anything like this before (besides the first-party tidbyt apps)?

Write your Pixlet application, containing a API call with a cached Time to Live (TTL) so it will call the API if the time of life is passed (data is old).
Refer to the bitcoin tracker for how it can work.
Every time you access localhost, it will serve the image. You will have the API called if the time of life expired on the data from the last API call.

Serve your pixlet app using the following

pixlet serve MyPixlet.star

Then visit in browser, I find Chrome works well:
http://localhost:8080/

Pixlet API Cache Code

data = cache.get(“data”)
if data != None:
print(“Hit! Displaying cached data.”)
else:
print(“Miss! Calling API.”)
resp = http.get(URL)
if resp.status_code != 200:
fail(“API request failed with status %d”, resp.status_code)

data = resp.body()
cache.set("data", data, ttl_seconds=TTL)

You update the Tidbyt by capturing the response served at localhost by pixlet, and sending to Tidbyt for display.

Here is the Curl I referred to earlier, to capture and save the image and then use that to serve to API

Shell Script

variable=$(curl -0 localhost:8080 | grep -Eo ‘,[^"]+"’ | tr -d ‘"’ | tr -d ‘,’)

curl -d '{"image”:${variable}}’ -H “Authorization: Bearer API-KEY>” https://api.tidbyt.com/v0/devices/DEVICE-ID/push

You could save the ‘variable’ or use it as you wish to display on webpage etc if you’d like

2 Likes

Hi @btj,
I forked pixlet a while ago for a personal project and made some changes. I added an “api” parameter which runs pixlet in API mode. Any .star file under ./applets/ directory will be available and executed every time it’s requested, being returned in base64 format (endpoint for each applet would be http://localhost:8080/applet/, where file is .star). Applets can be also viewed from the browser by accessing the http://localhost:8080/
e.g:

I haven’t updated it lately, so it’s a bit behind from the main pixlet repository, but should work fine. I’ll merge latest changes when I get a chance.

EDIT: Just saw your axilla project, which looks pretty interesting.

2 Likes

Hey @deyavito yeah I wanted something I could eventually setup and have running on its own without a local server.

Your project sounds really interesting! I had not considered running multiple applets at once before but that’s a really good idea. I might incorporate an option to do that on axilla.

Hi guys

Just food for thought, when using API in pixlet, you can have the application serve the rendered pixlet upon request. Additionally; you may cache the response from api so you are not calling api every time the image is rendered, for those limited apis which only so many calls can be made. The bitcoin Tutorial example is the way to go for api caching

Awesome work guys I can’t wait to try out these when I’m back home :slight_smile:

1 Like