Skip to content

Commit e9670cd

Browse files
committed
feat: added new page 'pandas.html'.
1 parent 0d32dcc commit e9670cd

9 files changed

Lines changed: 201 additions & 14 deletions

File tree

rsrcs/io_inputs/app/config/imports/main-pyscript.json renamed to rsrcs/io_inputs/app/config/imports/antigravity_launcher-pyscript.json

File renamed without changes.
File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"packages": ["pandas"]
3+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# # Code that ALWAYS get executed; either main, or imported as module from other script.
2+
# print(__name__)
3+
# print("---")
4+
5+
# LIBRARIES / PACKAGES IMPORTS.
6+
7+
from js import console # Allows usage of 'console.log()' function from JavaScript (JS).
8+
from pyscript import when # [PyScript] Allows usage of 'pyscript.when' decorator for Python functions, to handle events: 'https://docs.pyscript.net/2024.8.1/api/#pyscriptwhen'.
9+
from pyscript.web import page # [PyScript] Import 'pyscript.web.page' methods to interact with DOM (as an alternative to plain JS methods.): '', 'https://docs.pyscript.net/2024.8.1/api/#pyscriptwebpage'.
10+
from pyodide.http import open_url # Fetches URLs.
11+
import pandas as pd # Dataframes, and csv's.
12+
from pyscript import display # [PyScript] Displays content, using in-function intelligence for proper display: 'https://docs.pyscript.net/2024.8.1/api/#pyscriptdisplay'.
13+
14+
# Function to print LOG in both environments: Python, and browser console, via JS.
15+
# - IMPORTANT: requires import package 'js' ('console' module), to print in console.
16+
def UDFPrintLog(IMessage: str) -> None:
17+
18+
# Prints log in Python console.
19+
print(IMessage)
20+
# Prints log in JS console.
21+
console.log(IMessage)
22+
23+
return None
24+
25+
# Function to manage workflow when button with id '#button_load' is clicked.
26+
# - IMPORTANT: requires import package 'pyscript' ('when' module), to add decorators.
27+
@when("click", "#button_load")
28+
def UDFLoadFromURL(event) -> None:
29+
30+
page["#pyscript_output_dataset_table"][0].innerHTML = "" # Clears any previous result.
31+
input_dataset_url = page["#input_dataset_url"][0].value # Gets input value (not the 'innerHTML').
32+
33+
UDFPrintLog(f"[LOG] Fetching data from: '{input_dataset_url}'.")
34+
dataset_dataframe = pd.read_csv(open_url(input_dataset_url)) # Reads pandas dataframe, by firstly parsing the provided URL.
35+
36+
# Make visible pyscript output sections of dataset and console log. Elements used are 'div's, with 'display' property INITIALLY set to 'none' (or 'hidden' via direct property).
37+
# - Ref.: 'https://www.w3schools.com/css/css_display_visibility.asp'.
38+
page["#pyscript_output_dataset_section"][0].style["display"] = "block"
39+
page["#pyscript_output_console_log_section"][0].style["display"] = "block"
40+
41+
display(dataset_dataframe, target = "pyscript_output_dataset_table", append = "False") # INJECTS the dataframe in the 'innerHTML' of the target element. With 'append="False"', it REPLACES any previous content there might be, instead of appending it with 'div's if using 'append="True"' (DEFAULT, if ommitted).
42+
43+
# MAIN FUNCTION.
44+
def main():
45+
46+
page_message = "This example loads a remote CSV file into a Pandas dataframe, and displays it."
47+
input_dataset_url_default = "https://raw.githubusercontent.com/datasets/airport-codes/master/data/airport-codes.csv"
48+
49+
# Set 'DEFAULT' values, via DOM:
50+
page["#pyscript_output_page_message"][0].innerHTML = page_message # Since using a no-self closing element; e.g. 'div', content is placed as 'innerHTML'.
51+
page["#input_dataset_url"][0].value = input_dataset_url_default # BE CAREFUL! Since using a self-closing element; e.g. 'input', content is set to its value, not using 'innerHTML'. The latter will return empty; i.e. ''.
52+
53+
# CODE EXECUTION.
54+
55+
# Code executed ONLY when script is called directly; NOT IMPORTED from other script.
56+
if __name__ == '__main__':
57+
main()

rsrcs/io_inputs/app/config/web_pages/antigravity.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ <h1>Loading...</h1>
4646
<a class="header-nav-left_aligned-links_container-link" href="./home.html">Home</a>
4747
<a class="header-nav-left_aligned-links_container-link" href="./hello_world.html">Hello World</a>
4848
<a class="header-nav-left_aligned-links_container-link" href="./antigravity.html"><b>ANTIGRAVITY</b></a>
49+
<a class="header-nav-left_aligned-links_container-link" href="./pandas.html">Pandas</a>
4950
</div>
5051
</div>
5152
<div class="header-nav-links_container">
@@ -68,18 +69,18 @@ <h1>'Antigravity' Example.</h1>
6869
<br>
6970
<hr>
7071
<p><b>Validation of 'pyscript.config' parameters.</b></p>
71-
<!-- USe of 'span' element / tag to manipulate text; more specifi, as an inline element: 'https://www.freecodecamp.org/news/span-html-how-to-use-the-span-tag-with-css/', 'https://www.w3schools.com/tags/tag_span.asp'. -->
72-
<p><b>Config. files</b>: <span id="pyscript_output_config_files" class="main-content-pyscript_output_text"></span></p>
73-
<p><b>Config packages</b>: <span id="pyscript_output_config_packages" class="main-content-pyscript_output_text"></span></p>
7472
<script type="py" defer>
7573
from pyscript import display, config
7674
display(config.get("files"), target="pyscript_output_config_files")
7775
display(config.get("packages"), target="pyscript_output_config_packages")
7876
</script>
77+
<!-- Use of 'span' element / tag to manipulate text; more specific, as an inline element: 'https://www.freecodecamp.org/news/span-html-how-to-use-the-span-tag-with-css/', 'https://www.w3schools.com/tags/tag_span.asp'. -->
78+
<p><b>Config. files</b>: <span id="pyscript_output_config_files" class="main-content-pyscript_output_text"></span></p>
79+
<p><b>Config packages</b>: <span id="pyscript_output_config_packages" class="main-content-pyscript_output_text"></span></p>
7980
<br>
8081
<b>Based on xkcd: antigravity <a href="https://xkcd.com/353/" target="_blank">https://xkcd.com/353/</a>.</b>
8182
<br>
82-
<script type="py" src="./../imports/main.py" config="./../imports/main-pyscript.json" defer></script>
83+
<script type="py" src="./../imports/antigravity_launcher.py" config="./../imports/antigravity_launcher-pyscript.json" defer></script>
8384
<!--
8485
TODO: investigate how to redirect output to an html element.
8586
- So far, un-successful; e.g. use 'svg' element.
@@ -89,7 +90,7 @@ <h1>'Antigravity' Example.</h1>
8990
</section>
9091
</main>
9192
<footer class="footer">
92-
<p>Desarrollado por: a1t0ghb | <a href="https://github.com/a1t0ghb" target="_blank">https://github.com/a1t0ghb</a> | <a href="mailto:js101@a1t0.aleeas.com" target="_blank">js101@a1t0.aleeas.com</a></p>
93+
<p>Developed by: a1t0ghb | <a href="https://github.com/a1t0ghb" target="_blank">https://github.com/a1t0ghb</a> | <a href="mailto:js101@a1t0.aleeas.com" target="_blank">js101@a1t0.aleeas.com</a></p>
9394
</footer>
9495
</body>
9596
</html>

rsrcs/io_inputs/app/config/web_pages/hello_world.html

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ <h1>Loading...</h1>
4646
<a class="header-nav-left_aligned-links_container-link" href="./home.html">Home</a>
4747
<a class="header-nav-left_aligned-links_container-link" href="./hello_world.html"><b>HELLO WORLD</b></a>
4848
<a class="header-nav-left_aligned-links_container-link" href="./antigravity.html">Antigravity</a>
49+
<a class="header-nav-left_aligned-links_container-link" href="./pandas.html">Pandas</a>
4950
</div>
5051
</div>
5152
<div class="header-nav-links_container">
@@ -82,31 +83,31 @@ <h2>Using 'pyscript.display': default (i.e. 'append=True') vs. 'append=False'.</
8283
If running ALONE: 'pyscript.display' adds new html element '<script-py>'', right after <script>...</script>: '<script-py id="py-0"><div>...<result_from_pyscript01>...</div></script-py>'.
8384
- IMPORTANT: 'div' is added to ATTACH result, to ANY PREVIOUS inner content of the target DOM element; in this case '<script-py>', since target DOM wasn't specified in the 'pyscript.display' function.
8485
-->
85-
<p id="pyscript_tests_case01a" class="main-content-pyscript_output_text"></p>
8686
<script type="py" defer>
8787
from pyscript import display
8888
display("This is PyScript running... Default.", target="pyscript_tests_case01a")
8989
</script>
90+
<p id="pyscript_tests_case01a" class="main-content-pyscript_output_text"></p>
9091
<p><i>Default:</i></p>
9192
<!--
9293
When both 'script's run sequentially, it adds new html elements, but both results get placed at the end, RIGHT AFTER the LAST <script>...</script> EXECUTED (in case, target DOMs were NOT specified in the 'pyscript.display' functions):
9394
- For 1st script, html element '<script-py id="py-0"></script-py>', still gets created in-place as regular (i.e. after the corresponding '<script>' element), but EMPTY; i.e. '<script-py id="py-0"></script-py>'.
9495
- Outputs are ALL created and assigned to LAST html element '<script-py>' (in case, target DOMs were NOT specified in the 'pyscript.display' functions): '<script-py id="py-1"><div>...<result_from_pyscript01>...</div><div>...<result_from_pyscript02>...</div></script-py>'; id="py-0" to "py-1", in this case for x2 pyscripts executions.
9596
-->
96-
<p id="pyscript_tests_case01b" class="main-content-pyscript_output_text"></p>
9797
<script type="py" defer>
9898
from pyscript import display
9999
display("This is PyScript running... Default.", target="pyscript_tests_case01b")
100100
</script>
101+
<p id="pyscript_tests_case01b" class="main-content-pyscript_output_text"></p>
101102
<br>
102103
<p><b>CASE 02: default, 'append=False'.</b></p>
103104
<p><i>Default:</i></p>
104105
<!-- [As first example.] -->
105-
<p id="pyscript_tests_case02a" class="main-content-pyscript_output_text"></p>
106106
<script type="py" defer>
107107
from pyscript import display
108108
display("This is PyScript running... Default.", target="pyscript_tests_case02a")
109109
</script>
110+
<p id="pyscript_tests_case02a" class="main-content-pyscript_output_text"></p>
110111
</p><i>'append=False':</i></p>
111112
<!--
112113
If running ALONE: 'pyscript.display' adds new html element '<script-py>'', right after <script>...</script> (without <div>): '<script-py id="py-0">...<result_from_display>...</script-py>'.
@@ -117,22 +118,22 @@ <h2>Using 'pyscript.display': default (i.e. 'append=True') vs. 'append=False'.</
117118
- For 1st script, html element '<script-py id="py-0"></script-py>', still gets created in-place as regular (i.e. after the corresponding '<script>' element), but EMPTY; i.e. '<script-py id="py-0"></script-py>'.
118119
- Created outputs depend on which pyscripts were run with 'append=False'. Any pyscript as such, WILL OVERWRITE ANY PREVIOUS inner content of the target DOM (in case, target DOMs were NOT specified in the 'pyscript.display' functions); in this case, 1st pyscript is overwritten and only results from 2nd pyscript are showed: '<script-py id="py-1">...<result_from_pyscript02>...</script-py>'; id="py-0" to "py-1", in this case for x2 scripts executions.
119120
-->
120-
<p id="pyscript_tests_case02b" class="main-content-pyscript_output_text"></p>
121121
<script type="py" defer>
122122
from pyscript import display
123123
display("This is PyScript running... 'append=False'.", append=False, target="pyscript_tests_case02b")
124124
</script>
125+
<p id="pyscript_tests_case02b" class="main-content-pyscript_output_text"></p>
125126
<br>
126127
<hr>
127128
<h2>Example with 'pyscript.display': review 'pyscript.config' parameters.</h2>
128-
<!-- USe of 'span' element / tag to manipulate text; more specifi, as an inline element: 'https://www.freecodecamp.org/news/span-html-how-to-use-the-span-tag-with-css/', 'https://www.w3schools.com/tags/tag_span.asp'. -->
129-
<p><b>Config. files</b>: <span id="pyscript_output_config_files" class="main-content-pyscript_output_text"></span></p>
130-
<p><b>Config packages</b>: <span id="pyscript_output_config_packages" class="main-content-pyscript_output_text"></span></p>
131129
<script type="py" defer>
132130
from pyscript import display, config
133131
display(config.get("files"), target="pyscript_output_config_files")
134132
display(config.get("packages"), target="pyscript_output_config_packages")
135133
</script>
134+
<!-- Use of 'span' element / tag to manipulate text; more specific, as an inline element: 'https://www.freecodecamp.org/news/span-html-how-to-use-the-span-tag-with-css/', 'https://www.w3schools.com/tags/tag_span.asp'. -->
135+
<p><b>Config. files</b>: <span id="pyscript_output_config_files" class="main-content-pyscript_output_text"></span></p>
136+
<p><b>Config packages</b>: <span id="pyscript_output_config_packages" class="main-content-pyscript_output_text"></span></p>
136137
<br>
137138
<hr>
138139
<h2>Example with 'pyscript.display': display date and time, NOT USING 'target=' specification.</h2>
@@ -148,7 +149,7 @@ <h2>Example with 'pyscript.display': display date and time, NOT USING 'target='
148149
</section>
149150
</main>
150151
<footer class="footer">
151-
<p>Desarrollado por: a1t0ghb | <a href="https://github.com/a1t0ghb" target="_blank">https://github.com/a1t0ghb</a> | <a href="mailto:js101@a1t0.aleeas.com" target="_blank">js101@a1t0.aleeas.com</a></p>
152+
<p>Developed by: a1t0ghb | <a href="https://github.com/a1t0ghb" target="_blank">https://github.com/a1t0ghb</a> | <a href="mailto:js101@a1t0.aleeas.com" target="_blank">js101@a1t0.aleeas.com</a></p>
152153
</footer>
153154
</body>
154155
</html>

rsrcs/io_inputs/app/config/web_pages/home.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ <h1>Loading...</h1>
4646
<a class="header-nav-left_aligned-links_container-link" href="./home.html"><b>HOME</b></a>
4747
<a class="header-nav-left_aligned-links_container-link" href="./hello_world.html">Hello World</a>
4848
<a class="header-nav-left_aligned-links_container-link" href="./antigravity.html">Antigravity</a>
49+
<a class="header-nav-left_aligned-links_container-link" href="./pandas.html">Pandas</a>
4950
</div>
5051
</div>
5152
<div class="header-nav-links_container">
@@ -70,7 +71,7 @@ <h1>PyScript Exercises.</h1>
7071
</section>
7172
</main>
7273
<footer class="footer">
73-
<p>Desarrollado por: a1t0ghb | <a href="https://github.com/a1t0ghb" target="_blank">https://github.com/a1t0ghb</a> | <a href="mailto:js101@a1t0.aleeas.com" target="_blank">js101@a1t0.aleeas.com</a></p>
74+
<p>Developed by: a1t0ghb | <a href="https://github.com/a1t0ghb" target="_blank">https://github.com/a1t0ghb</a> | <a href="mailto:js101@a1t0.aleeas.com" target="_blank">js101@a1t0.aleeas.com</a></p>
7475
</footer>
7576
</body>
7677
</html>

rsrcs/io_inputs/app/config/web_pages/home_(style).css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ ul {
136136
color: var(--colors_04);
137137
}
138138

139+
#pyscript_output_console_log_output {
140+
max-width: 750px;
141+
}
142+
139143
.footer {
140144
background-color: var(--colors_01);
141145
color: var(--colors_background);

0 commit comments

Comments
 (0)