Last active
June 9, 2022 02:11
-
-
Save iuriguilherme/ea5a5518e27590b8a93ac15091dfc68a to your computer and use it in GitHub Desktop.
Pyscript showcase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang = "en"> | |
<head> | |
<!-- | |
This is mandatory for latin characters, because there's just no standard | |
in web browsers, and probably there'll never be | |
--> | |
<meta charset = "utf-8" /> | |
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> | |
<script defer src="https://pyscript.net/alpha/pyscript.js"></script> | |
</head> | |
<body> | |
<div class = "grid grid-flow-col auto-cols-auto gap-3"> | |
<div> </div> | |
<div class = "grid grid-flow-row grid-auto-rows-max gap-6"> | |
<div>HTTP requesting support tests results will display here:</div> | |
<div id = 'http_result'></div> | |
<hr> | |
<div> | |
Pyscript console: | |
</div> | |
<div> | |
<div class = "grid gap-3"> | |
<div class = "bg-neutral-600"> <!-- y u no work --> | |
<py-script> | |
""" | |
Pyscrypt showcase - trying to test features working and not working on | |
https://pyscript.net | |
""" | |
import logging | |
logging.basicConfig(level = 'DEBUG') | |
logger = logging.getLogger('index.html') | |
import asyncio | |
import micropip | |
import os | |
import sys | |
logger.info(f""" | |
Running {__name__} script | |
System: {sys.version} | |
Environment: {os.environ} | |
""") | |
logger.info("""Trying to make the hello world trick from \ | |
https://codegolf.stackexchange.com/questions/55422/hello-world/218190#218190\ | |
""") | |
try: | |
try: | |
import qz | |
except: | |
logger.debug("importing qz didn't work, trying with micropip...") | |
await micropip.install('qz') | |
import qz | |
except Exception as e: | |
logger.warning("importing qz did not work whatsoever") | |
logger.exception(e) | |
logger.info("HTTP requests support test") | |
url = "https://httpbin.org/get" | |
logger.info("Trying aiohttp...") | |
try: | |
try: | |
import aiohttp | |
except: | |
await micropip.install('aiohttp', keep_going = True) | |
import aiohttp | |
with aiohttp.ClientSession() as session: | |
with session.get(url) as response: | |
html = response.text() | |
pyscript.write('http_result', f""" | |
AioHTTP test results: | |
Status: {response.status} | |
Content-type: {response.headers['content-type']} | |
Body: {html[:15]} | |
""") | |
except Exception as e: | |
logger.warning("trying to use aiohttp failed") | |
logger.exception(e) | |
logger.info("Trying urllib3...") | |
try: | |
try: | |
import urllib3 | |
except: | |
await micropip.install('urllib3') | |
import urllib3 | |
http = urllib3.PoolManager() | |
r = http.request('GET', url) | |
pyscript.write('http_result', f""" | |
urllib3 test results: | |
Status: {r.status} | |
Content-type: {r.headers['content-type']} | |
Data: {r.data} | |
""") | |
except Exception as e: | |
logger.warning("trying to use urllib3 failed") | |
logger.exception(e) | |
logger.info("Trying requests...") | |
try: | |
try: | |
import requests | |
except: | |
await micropip.install('requests') | |
import requests | |
r = requests.get(url) | |
pyscript.write('http_result', f""" | |
requests tests results: | |
Status: {r.status_code} | |
Content-type: {r.headers['content-type']} | |
Data: {r.data} | |
""") | |
except Exception as e: | |
logger.warning("trying to use requests failed") | |
logger.exception(e) | |
logger.info("Trying pyfetch...") | |
try: | |
from pyodide.http import pyfetch | |
r = await pyfetch(url = url, method = "GET") | |
pyscript.write('http_result', f""" | |
pyfetch tests results: | |
Status: {r.status} {r.status_text} | |
Json: {await r.json()} | |
""") | |
except Exception as e: | |
logger.warning("trying to use pyfetch failed") | |
logger.exception(e) | |
</py-script> | |
</div> <!-- "bg-neutral-600" --> | |
</div> <!-- "grid gap-3" --> | |
</div> | |
</div> | |
</div> <!-- "grid grid-flow-row grid-auto-rows-max gap-6" --> | |
<div> </div> | |
</div> <!-- "grid grid-flow-col auto-cols-auto gap-3" --> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment