Skip to content

Instantly share code, notes, and snippets.

@simonw
Created February 2, 2024 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonw/7d5e3e45401e5cfe91be08ee122dc4bf to your computer and use it in GitHub Desktop.
Save simonw/7d5e3e45401e5cfe91be08ee122dc4bf to your computer and use it in GitHub Desktop.

I made this Toga app as a Bash one-liner:

python -c '
import toga
import urllib.request
import json

def fetch_json(url):
    with urllib.request.urlopen(url) as response:
        if response.getcode() == 200:
            data = response.read()
            return json.loads(data.decode("utf-8"))
        else:
            raise urllib.error.URLError(f"Failed to retrieve data. HTTP Error Code: {response.getcode()}")


class TogaDemo(toga.App):
    def startup(self):
        # Create the main window
        self.main_window = toga.MainWindow()

        data = fetch_json("https://datasette.io/content/datasette_repos.json")

        table = toga.Table(
            headings=data["columns"],
            data=data["rows"]
        )
        self.main_window.content = table
        self.main_window.show()


TogaDemo("Datasette Table", "io.datasette.toga-datasette").main_loop()
'
@simonw
Copy link
Author

simonw commented Feb 2, 2024

CleanShot 2024-02-01 at 19 02 05@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment