Lädt...


🔧 Python in the Browser, Fetching JSON from an AWS S3 bucket into a Bokeh Line Chart, a Serverless solution


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

I needed some quick Python to replace broken code over in AWS Lambda. While looking at IDEs I stumbled upon a way to write and run Python directly in the Browser just with a Notepad. So I opened Notepad and wrote a boiler plate html file with a Python Library called Pyscript.js. There was no IDE or server involved, just my frontend file. All I had to do was double click the HTML file and my Python, Javascript, JSON and HTML played very nicely together.

Also I had recently ditched React in favor of Plain JS and started fetching JSON from S3 versus the traditional SQL approach. So Fetch in Python? Was this possible? Fetch is JS and Python is not, so I thought. But thanks to WebAssembly now we can combine both Python and Javascript on an HTML page.

Since I never really used much Python and I was mostly Plain JS without Node I was accustomed to using CDN Libraries in my HTML. I was drawing some line charts one day and got to thinking about hosting them in JS too. Bokeh was the answer. Bokeh is a Python library and a JS Library so it fit my new configuration. Now my solution was totally serverless.

So lets look at some Pyscript now. It has only been around since 2022. In 31 lines of code I can do all the above. The only drawback is that my chart takes 8 seconds to load and from Python IDLE it takes 3 seconds. But the data is worth waiting for because of the value it adds to our Marketing effort. Recently Pyscript had a complete rewrite. This code used to be 60 lines just 2 months ago and prior to that it was 100+ lines using popular competitor Matplotlib.

So now the no brainer code written in pyscript newest release, June 2024
Note: this is a first for this newest way of writing pyscript as of 6-15-24
Beware that versioning is critical with Pyscript and Bokeh and is not backward compatible.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>get json from AWS S3</title>
    <link rel="stylesheet" href="https://pyscript.net/releases/2024.6.1/core.css"> <!--most recent pyscript lib-->
    <script type="module" src="https://pyscript.net/releases/2024.6.1/core.js"></script> <!--most recent pyscript lib-->
    <script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-3.4.1.min.js"></script> <!--recent bokeh lib-->
    <py-config>packages = ["bokeh"]</py-config>
</head>
<body>
    <h1>wait 8 seconds to display Bokeh Line Chart</p>
    <div id="chart"></div> <!--inside the script tags below is my Python code-->
    <script type="py" async>
        from bokeh.plotting import figure
        from bokeh.embed import json_item
        from pyscript import window, fetch, ffi 
        Bokeh = window.Bokeh #pass Bokeh into the main threads window object
        async def get_data():
            response = await fetch('https://rickd.s3.us-east-2.amazonaws.com/04.json')
            data = await response.json()
            xAxis=[i['time'] for i in data] #grab s3 data using loop
            yAxis=[i['Quantity'] for i in data] #grab s3 data using loop
            p = figure(width = 700, height = 600) #beware that much code u see still uses plot_width /height which does not work anymore
            p.line(xAxis, yAxis, legend_label="data from AWS S3", line_width=2)
            await Bokeh.embed.embed_item(ffi.to_js(json_item(p, "chart"))) #ffi converts a Python object into its JavaScript counterpart
        await get_data()
    </script>
</body>
</html>

Happy coding folks!

...

🔧 Python in the Browser, Fetching JSON from an AWS S3 bucket into a Bokeh Line Chart, a Serverless solution


📈 124.89 Punkte
🔧 Programmierung

🔧 Create Chart in React Using Chart.js || Chart JS Tutorial React


📈 40.47 Punkte
🔧 Programmierung

📰 Chart Wars: Pie Chart vs. Sorted Radial Bar Chart


📈 40.47 Punkte
🔧 AI Nachrichten

🔧 The combination chart and line chart are blocked, causing the tooltip to not be hover.


📈 35.19 Punkte
🔧 Programmierung

🔧 KISS Principle: Giữ Mọi Thứ Đơn Giản Nhất Có Thể


📈 31.25 Punkte
🔧 Programmierung

🔧 Có thể bạn chưa biết (Phần 1)


📈 31.25 Punkte
🔧 Programmierung

🔧 Tìm Hiểu Về RAG: Công Nghệ Đột Phá Đang "Làm Mưa Làm Gió" Trong Thế Giới Chatbot


📈 31.25 Punkte
🔧 Programmierung

🔧 How to draw a line chart with a multi-line layout


📈 29.91 Punkte
🔧 Programmierung

🔧 Bokeh an interesting data tool in python for data visualization


📈 27.68 Punkte
🔧 Programmierung

🔧 Deploying Bokeh Dashboard with Python on VPS: A Step-by-Step Guide


📈 27.68 Punkte
🔧 Programmierung

📰 8 Tips for Creating Data Visualizations in Python using Bokeh


📈 27.68 Punkte
🔧 AI Nachrichten

📰 heise+ | Python: Interaktive Web-Grafiken mit Bokeh und Pandas erstellen


📈 27.68 Punkte
📰 IT Nachrichten

🔧 A Guide to Creating a Serverless Function for Fetching External Data on Hubspot


📈 27.12 Punkte
🔧 Programmierung

🔧 How to visualize bar chart with react-chart-2, showing label on the bar


📈 26.98 Punkte
🔧 Programmierung

📰 Chart Wars — Stacked Bar Chart vs. Heatmap


📈 26.98 Punkte
🔧 AI Nachrichten

🔧 How to use Chart.js to create a Chart for Your Project


📈 26.98 Punkte
🔧 Programmierung

🕵️ Medium CVE-2020-15943: Gantt-chart project Gantt-chart


📈 26.98 Punkte
🕵️ Sicherheitslücken

🕵️ Low CVE-2020-15944: Gantt-chart project Gantt-chart


📈 26.98 Punkte
🕵️ Sicherheitslücken

📰 From Default Python Line Chart to Journal-Quality Infographics


📈 26.86 Punkte
🔧 AI Nachrichten

🔧 Some Websites for hosting and fetching JSON


📈 26.59 Punkte
🔧 Programmierung

🔧 How to create serverless applications with AWS SAM (Serverless Application Model)


📈 26.42 Punkte
🔧 Programmierung

🔧 Build a highly scalable Serverless CRUD Microservice with AWS Lambda and the Serverless Framework


📈 26.42 Punkte
🔧 Programmierung

🔧 Build a highly scalable Serverless CRUD Microservice with AWS Lambda and the Serverless Framework


📈 26.42 Punkte
🔧 Programmierung

🔧 AWS SAM vs. Serverless Framework: Choosing the Right Framework for Serverless Development


📈 26.42 Punkte
🔧 Programmierung

🔧 Exploring AWS OpenSearch Serverless Pricing: How It Differs from Traditional Serverless Services


📈 26.42 Punkte
🔧 Programmierung

🔧 Exploring AWS OpenSearch Serverless Pricing: How It Differs from Traditional Serverless Services


📈 26.42 Punkte
🔧 Programmierung

matomo