Skip to content

Instantly share code, notes, and snippets.

@ProfAndreaPollini
Created July 10, 2022 10:20
Show Gist options
  • Save ProfAndreaPollini/ec1a73fc384de5dea7c3ff16a912a970 to your computer and use it in GitHub Desktop.
Save ProfAndreaPollini/ec1a73fc384de5dea7c3ff16a912a970 to your computer and use it in GitHub Desktop.
Pyscript bitcoin tracker
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Home</title>
<script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.js"></script>
<script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.4.2.min.js"></script>
<script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.4.2.min.js"></script>
<script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.4.2.min.js"></script>
<script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-2.4.2.min.js"></script>
<script type="text/javascript">
Bokeh.set_log_level("info");
</script>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<py-env>
- matplotlib
- numpy
</py-env>
</head>
<body>
<h1>Bitcoin tracker</h1>
<div id="plot"></div>
<div id="btcplot"></div>
<py-script output="plot">
import js
from js import document
import matplotlib.pyplot as plt
import json
from pyodide.http import open_url
BTC_URI = "https://api.coindesk.com/v1/bpi/currentprice.json"
data = json.loads(open_url(BTC_URI).getvalue())
data = float(data["bpi"]["EUR"]["rate"].replace(",", ""))
from pyodide import create_proxy
btcdata = []
interval_id = 0
def myFunc():
global btcdata, interval_id
BTC_URI = "https://api.coindesk.com/v1/bpi/currentprice.json"
data = json.loads(open_url(BTC_URI).getvalue())
data = float(data["bpi"]["EUR"]["rate"].replace(",", ""))
btcdata.append(data)
x = list(range(len(btcdata)))
y = btcdata
fig, ax = plt.subplots()
ax.plot(x, y)
pyscript.write("btcplot",fig)
# Create a proxy for the callback
proxy = create_proxy(myFunc)
interval_id = js.setInterval(proxy, 10000);
</py-script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment