├── LICENSE ├── README.md └── examples ├── Readme.md ├── pyscript_pandas.html └── pyscript_random_gen.html /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Tirthajyoti Sarkar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PyScript-examples 2 | Examples of web pages developed with PyScript framework 3 | 4 | ## What is PyScript? 5 | From their [Github repo](https://github.com/pyscript/pyscript) 6 | > PyScript is a meta project that aims to combine multiple open technologies to create a framework for users to use Python (and other languages) to create sophisticated applications in the browser. It highly integrates with the way the DOM works in the browser and allows users to add logic, in Python, in a way that feels natural to web as well as Python developers. 7 | 8 | ## No installation needed 9 | To write Python code inside your HTML, just add these assets in your script inside the `
...` 10 | ``` 11 | 12 | 13 | ``` 14 | -------------------------------------------------------------------------------- /examples/Readme.md: -------------------------------------------------------------------------------- 1 | ## Examples 2 | - Random number generation using NumPy ([HTML source](https://github.com/tirthajyoti/PyScript-examples/blob/main/examples/pyscript_random_gen.html), [Github page](https://tirthajyoti.github.io/pyscript_random_gen.html)) 3 | - Basic Pandas demo ([HTML source](https://github.com/tirthajyoti/PyScript-examples/blob/main/examples/pyscript_pandas.html), [Github page](https://tirthajyoti.github.io/pyscript_pandas.html)) 4 | -------------------------------------------------------------------------------- /examples/pyscript_pandas.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |Demo developed by Dr. Tirthajyoti sarkar
62 | Check out the announcement from Anaconda about PyScript framework 63 |To provide the power of Python on your browser, this technology needs to load a special virtual machine and all necessary Python modules
68 |So, sit back and relax. We are loading the modules and generating some random variables as a test. 69 | You can start interacting with the page when you see a histogram of those random variates displayed below.
70 |We promise that, once loaded, your interactions will be Python-powered and fast! You can also read more about this awesome framework here at Anaconda's engineering blog.
71 | Anaconda Engineering Blog about PyScript 72 |PyScript loading and initializing...
77 |style
section. The Pandas pd.to_html()
94 | function has a classes
argument that accepts a CSS class or a list of classes. We utilize that formatting this table.document.getElementbyId()
function to accomplish this and read back that HTML into Pandas using the pd.read_html()
function.
105 | Demo developed by Dr. Tirthajyoti sarkar
23 | Check out the announcement from Anaconda about PyScript framework 24 |To provide the power of Python on your browser, this technology needs to load a special virtual machine and all necessary Python modules
29 |So, sit back and relax. We are loading the modules and generating some random variables as a test. 30 | You can start interacting with the page when you see a histogram of those random variates displayed below.
31 |We promise that, once loaded, your interactions will be Python-powered and fast! You can also read more about this awesome framework here at Anaconda's engineering blog.
32 | Anaconda Engineering Blog about PyScript 33 |NumPy
We literally wrote this pure Python code in the HTML file to generate these numbers! If you don't believe us, just right-click and view the source code of this page :-)
37 | The only thing to note here is the <py-script></py-script>
tags around the code block.
38 |
40 | <py-script>
41 | import numpy as np
42 | integers = np.random.randint(0,100,100)
43 | print(integers)
44 | </py-script>
45 | Matplotlib
You can do all the usual Matplotlib
tweaks and tricks with the visualization as you would in a normal Python script or Jupyter Notebook.
53 | Here, we have just set the dpi=150
and figsize=(5,2)
inside the plt.subplots()
function.
54 |
This code is inline too i.e., embedded with this HTML. So, feel free to check it out. The last line of the Python code block is just the figure object fig1
.
55 | This tells the PyScript framework to output the object/image (it detects the type intelligently) to the corresponding div
section.
56 |
In the next section, we generate histogram based on the type of the distribution that the user (you) choses.
69 | We slightly change the output method for this
70 | as we grab the div
element where we want to display the image and write the figure object with a code like out.write(fig1)
As you expect, every time the button is clicked, you will see a new histogram. 90 | All that computation is happening with pure Python code and in your browser!
91 | 96 |Adjust the number of random variates
98 | 100 | 101 |