diff --git a/docs/tutorials/requests.md b/docs/tutorials/requests.md index e0c2aa9d..e03faee4 100644 --- a/docs/tutorials/requests.md +++ b/docs/tutorials/requests.md @@ -72,7 +72,7 @@ Now that we have installed the dependencies, we need to patch the Requests libra import pyodide_http - pyodide_http.patch() + pyodide_http.patch_all() + Get current time + +

+ + + from pyscript import Element + import datetime + + def current_time(): + now = datetime.datetime.now() + + # Get paragraph element by id + paragraph = Element("current-time") + + # Add current time to the paragraph element + paragraph.write(now.strftime("%Y-%m-%d %H:%M:%S")) + + """ + ) + + assert self.console.log.lines[0] == self.PY_COMPLETE + btn = self.page.wait_for_selector("#get-time") + btn.click() + + current_time = self.page.wait_for_selector("#current-time") + + pattern = "\\d+-\\d+-\\d+\\s\\d+:\\d+:\\d+" # e.g. 08-09-2022 15:57:32 + assert re.search(pattern, current_time.inner_text()) + self.assert_no_banners() + + def test_tutorials_requests(self): + self.pyscript_run( + """ + + packages = ["requests", "pyodide-http"] + + + + import requests + import pyodide_http + + # Patch the Requests library so it works with Pyscript + pyodide_http.patch_all() + + # Make a request to the JSON Placeholder API + response = requests.get("https://jsonplaceholder.typicode.com/todos") + print(response.json()) + + """ + ) + + assert self.console.log.lines[0] == self.PY_COMPLETE + py_terminal = self.page.wait_for_selector("py-terminal") + # Just a small check to confirm that the response was received + assert "userId" in py_terminal.inner_text() + self.assert_no_banners() + + def test_tutorials_py_config_fetch(self): + # flake8: noqa + self.pyscript_run( + """ + + [[fetch]] + from = "https://pyscript.net/examples/" + files = ["utils.py"] + [[fetch]] + from = "https://gist.githubusercontent.com/FabioRosado/faba0b7f6ad4438b07c9ac567c73b864/raw/37603b76dc7ef7997bf36781ea0116150f727f44/" + files = ["todo.py"] + + + from todo import add_task, add_task_event + +
+
+

+ To Do List +

+
+
+ + +
+
+ Fold laundry

' + in first_task.inner_html() + ) + self.assert_no_banners() + + def test_tutorials_py_config_interpreter(self): + self.pyscript_run( + """ + + [[interpreters]] + src = "https://cdn.jsdelivr.net/pyodide/v0.22.0a3/full/pyodide.js" + name = "pyodide-0.22.0a3" + lang = "python" + + + import pyodide + print(pyodide.__version__) + + """ + ) + + assert self.console.log.lines[0] == self.PY_COMPLETE + py_terminal = self.page.wait_for_selector("py-terminal") + assert "0.22.0a3" in py_terminal.inner_text() + self.assert_no_banners() + + def test_tutorials_writing_to_page(self): + self.pyscript_run( + """ +
+ +
+ +
+ +
+ + + + def write_to_page(): + manual_div = Element("manual-write") + manual_div.element.innerHTML = "

Hello World

" + + def display_to_div(): + display("I display things!", target="display-write") + + def print_to_page(): + print("I print things!") +
+ """ + ) + assert self.console.log.lines[0] == self.PY_COMPLETE + btn_manual = self.page.wait_for_selector("#manual") + btn_display = self.page.wait_for_selector("#display") + btn_print = self.page.wait_for_selector("#print") + + btn_manual.click() + manual_write_div = self.page.wait_for_selector("#manual-write") + assert "

Hello World

" in manual_write_div.inner_html() + + btn_display.click() + display_write_div = self.page.wait_for_selector("#display-write") + assert "I display things!" in display_write_div.inner_text() + + btn_print.click() + py_terminal = self.page.wait_for_selector("py-terminal") + assert "I print things!" in py_terminal.inner_text() + self.assert_no_banners() + + def test_guides_asyncio(self): + self.pyscript_run( + """ + + import asyncio + + async def main(): + for i in range(3): + print(i) + + asyncio.ensure_future(main()) + + """ + ) + assert self.console.log.lines[0] == self.PY_COMPLETE + py_terminal = self.page.wait_for_selector("py-terminal") + + assert "0\n1\n2\n" in py_terminal.inner_text()