Skip to content

Instantly share code, notes, and snippets.

@liquidcarbon
Last active November 22, 2024 13:18
Show Gist options
  • Save liquidcarbon/8574249103648c4306c22a2e60c486e1 to your computer and use it in GitHub Desktop.
Save liquidcarbon/8574249103648c4306c22a2e60c486e1 to your computer and use it in GitHub Desktop.
Marimo Chuck Norris Joke Dashboard
# video: https://github.com/user-attachments/assets/7e364faf-ddac-4acc-b5e6-83e61c9ed2de
import marimo
__generated_with = "0.9.20"
app = marimo.App(width="medium")
@app.cell
def __():
import duckdb
import httpx
import marimo as mo
api_urls = [
"https://api.chucknorris.io/jokes/search?query=lemon",
"https://api.chucknorris.io/jokes/search?query=friday",
"https://api.chucknorris.io/bad-url",
]
return api_urls, duckdb, httpx, mo
@app.cell
def __(api_urls, duckdb, httpx, mo):
# lists hold state
response = [httpx.get(api_urls[0])]
query = ["SELECT UNNEST(result)->'$.value' AS joke\nFROM READ_JSON_AUTO('{api_url}')"]
result = [duckdb.sql(query[0].format(api_url=api_urls[0])).df().sample(3)]
img = [mo.image(f"https://http.cat/{response[0].status_code}", width=240)]
# UI with initial values
url_input = mo.ui.dropdown(label="API URL: ", options=api_urls, value=api_urls[0], full_width=True)
sql_editor = mo.ui.code_editor(language="sql", min_height=30, value=query[0])
run_button = mo.ui.run_button(label="run", keyboard_shortcut="Alt+R")
return img, query, response, result, run_button, sql_editor, url_input
@app.cell
def __(
duckdb,
httpx,
img,
mo,
query,
response,
result,
run_button,
sql_editor,
url_input,
):
if run_button.value:
response[0] = httpx.get(url_input.value)
query[0] = sql_editor.value.format(api_url=url_input.value)
try:
result[0] = duckdb.sql(query[0]).df().sample(3)
except:
result[0] = {"joke": ["not found"]}
img[0] = mo.image(f"https://http.cat/{response[0].status_code}", width=240)
return
@app.cell
def __(img, mo, query, result, run_button, sql_editor, url_input):
mo.vstack([
url_input,
sql_editor,
run_button,
query[0],
img[0],
mo.ui.table(result[0], wrapped_columns=["joke"]),
]),
return
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment