From 37440f7eb844ee8b91724899fef8ebe39148ed3c Mon Sep 17 00:00:00 2001 From: Aayush Badoni Date: Sat, 30 Mar 2024 15:33:12 +0530 Subject: [PATCH 1/5] basic pyscript component --- _sources/index_en.rst | 2 ++ _sources/poc/poc_en.rst | 65 +++++++++++++++++++++++++++++++++++++++++ build_info | 1 + 3 files changed, 68 insertions(+) create mode 100644 _sources/poc/poc_en.rst create mode 100644 build_info diff --git a/_sources/index_en.rst b/_sources/index_en.rst index 07582b56c2..70355874de 100644 --- a/_sources/index_en.rst +++ b/_sources/index_en.rst @@ -54,6 +54,8 @@ Contents: challenges/Reto03_en.rst challenges/Reto04_en.rst challenges/Reto05_en.rst + poc/poc_en.rst + poc/poc_1.rst diff --git a/_sources/poc/poc_en.rst b/_sources/poc/poc_en.rst new file mode 100644 index 0000000000..aeec4b6fa6 --- /dev/null +++ b/_sources/poc/poc_en.rst @@ -0,0 +1,65 @@ +======================================== +PyScript - Running Python in the Browser +======================================== + +Introduction to PyScript +------------------------- +PyScript is a Python interpreter that runs in the browser. It is based on Piodide and supports all the modules that are available in Piodide. +PyScript is a great tool for learning Python, experimenting with Python code, and sharing Python code with others. +PyScript is open source and can be used for free. + +Getting Started +--------------- +To get started with PyScript, you can simply start typing Python code in the code editor below. +You can run the code by pressing ``Shift + Enter`` on your keyboard. I plan to add a run button later. + +PyScript provides an interactive Python shell that you can use to run Python code. +It is Provided by ``py-repl`` tag, this has been changed to ``script type="py-editor"`` in the latest release. +I plan to use the latest stable release but for this POC we'll stick to ``py-repl`` tag. + +Feel free to interact with the Python shell and run Python code in the code editor below. +Happy Coding! + +.. raw:: html + +
+ + + PyFront - Simple + + + + + + + + packages = ["numpy", "pandas", "matplotlib"] + terminal = true + + + + import pandas as pd + import numpy as np + import matplotlib.pyplot as plt + + # Dummy data for sales performance of products in different regions + regions = ['North', 'South', 'East', 'West'] + products = ['Product A', 'Product B', 'Product C', 'Product D', 'Product E'] + sales_data = pd.DataFrame(np.random.randint(1000, 5000, size=(len(regions), len(products))), columns=products, index=regions) + + def plot(data): + plt.rcParams["figure.figsize"] = (12, 8) + data.plot(kind='bar', stacked=True) + plt.xlabel('Regions') + plt.ylabel('Sales') + plt.title('Sales Performance of Products in Different Regions') + plt.xticks(rotation=0) + plt.legend(title='Products', bbox_to_anchor=(1.05, 1), loc='upper left') + plt.show() + + # Initial plot + plot(sales_data) + sales_data + + + diff --git a/build_info b/build_info new file mode 100644 index 0000000000..1d0fe84223 --- /dev/null +++ b/build_info @@ -0,0 +1 @@ +unknown-0-0 From 30eed5c7b6ba4a3ccd954c376afcb6ce07f99a77 Mon Sep 17 00:00:00 2001 From: Aayush Badoni Date: Sat, 30 Mar 2024 15:37:44 +0530 Subject: [PATCH 2/5] minor changes --- _sources/index_en.rst | 1 - build_info | 1 - 2 files changed, 2 deletions(-) delete mode 100644 build_info diff --git a/_sources/index_en.rst b/_sources/index_en.rst index 70355874de..22a6ca2968 100644 --- a/_sources/index_en.rst +++ b/_sources/index_en.rst @@ -55,7 +55,6 @@ Contents: challenges/Reto04_en.rst challenges/Reto05_en.rst poc/poc_en.rst - poc/poc_1.rst diff --git a/build_info b/build_info deleted file mode 100644 index 1d0fe84223..0000000000 --- a/build_info +++ /dev/null @@ -1 +0,0 @@ -unknown-0-0 From 851033b098a735aa1678d3e2a9b595df0b35fc93 Mon Sep 17 00:00:00 2001 From: Aayush Badoni Date: Sun, 31 Mar 2024 05:52:49 +0530 Subject: [PATCH 3/5] minor changes --- _sources/poc/_static/component.html | 26 ++++++++++ _sources/poc/poc_en.rst | 79 +++++++++++++++-------------- build_info | 1 + 3 files changed, 67 insertions(+), 39 deletions(-) create mode 100644 _sources/poc/_static/component.html create mode 100644 build_info diff --git a/_sources/poc/_static/component.html b/_sources/poc/_static/component.html new file mode 100644 index 0000000000..16a7f1a221 --- /dev/null +++ b/_sources/poc/_static/component.html @@ -0,0 +1,26 @@ +
+ +
diff --git a/_sources/poc/poc_en.rst b/_sources/poc/poc_en.rst index aeec4b6fa6..790f6d203f 100644 --- a/_sources/poc/poc_en.rst +++ b/_sources/poc/poc_en.rst @@ -18,48 +18,49 @@ It is Provided by ``py-repl`` tag, this has been changed to ``script type="py-ed I plan to use the latest stable release but for this POC we'll stick to ``py-repl`` tag. Feel free to interact with the Python shell and run Python code in the code editor below. -Happy Coding! +Happy coding! -.. raw:: html +Useful Information +------------------ +Try Running the code below to see a simple example of Python code running in the browser. +This uses the ``matplotlib``, ``numpy``, and ``pandas`` library to plot a graph. +A Brython environment can not run this code, but PyScript can. + +.. code:: python + + import matplotlib.pyplot as plt + import matplotlib.tri as tri + import numpy as np + + # First create the x and y coordinates of the points. + n_angles = 36 + n_radii = 8 + min_radius = 0.25 + radii = np.linspace(min_radius, 0.95, n_radii) -
- - - PyFront - Simple - - - - - + angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False) + angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1) + angles[:, 1::2] += np.pi / n_angles - - packages = ["numpy", "pandas", "matplotlib"] - terminal = true - - - - import pandas as pd - import numpy as np - import matplotlib.pyplot as plt + x = (radii * np.cos(angles)).flatten() + y = (radii * np.sin(angles)).flatten() + z = (np.cos(radii) * np.cos(3 * angles)).flatten() - # Dummy data for sales performance of products in different regions - regions = ['North', 'South', 'East', 'West'] - products = ['Product A', 'Product B', 'Product C', 'Product D', 'Product E'] - sales_data = pd.DataFrame(np.random.randint(1000, 5000, size=(len(regions), len(products))), columns=products, index=regions) + # Create the Triangulation; no triangles so Delaunay triangulation created. + triang = tri.Triangulation(x, y) - def plot(data): - plt.rcParams["figure.figsize"] = (12, 8) - data.plot(kind='bar', stacked=True) - plt.xlabel('Regions') - plt.ylabel('Sales') - plt.title('Sales Performance of Products in Different Regions') - plt.xticks(rotation=0) - plt.legend(title='Products', bbox_to_anchor=(1.05, 1), loc='upper left') - plt.show() + # Mask off unwanted triangles. + triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1), + y[triang.triangles].mean(axis=1)) + < min_radius) - # Initial plot - plot(sales_data) - sales_data - - - + fig1, ax1 = plt.subplots() + ax1.set_aspect('equal') + tpc = ax1.tripcolor(triang, z, shading='flat') + fig1.colorbar(tpc) + ax1.set_title('tripcolor of Delaunay triangulation, flat shading') + + display(fig1) + +.. raw:: html + :file: ./_static/component.html \ No newline at end of file diff --git a/build_info b/build_info new file mode 100644 index 0000000000..1d0fe84223 --- /dev/null +++ b/build_info @@ -0,0 +1 @@ +unknown-0-0 From 401c97f30888666af218843183b7089011594f0a Mon Sep 17 00:00:00 2001 From: Aayush Badoni Date: Sun, 31 Mar 2024 05:59:45 +0530 Subject: [PATCH 4/5] added graphs --- _sources/poc/poc_en.rst | 48 +++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/_sources/poc/poc_en.rst b/_sources/poc/poc_en.rst index 790f6d203f..2c345ae353 100644 --- a/_sources/poc/poc_en.rst +++ b/_sources/poc/poc_en.rst @@ -28,39 +28,25 @@ A Brython environment can not run this code, but PyScript can. .. code:: python + import pandas as pd import matplotlib.pyplot as plt - import matplotlib.tri as tri - import numpy as np - # First create the x and y coordinates of the points. - n_angles = 36 - n_radii = 8 - min_radius = 0.25 - radii = np.linspace(min_radius, 0.95, n_radii) - - angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False) - angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1) - angles[:, 1::2] += np.pi / n_angles - - x = (radii * np.cos(angles)).flatten() - y = (radii * np.sin(angles)).flatten() - z = (np.cos(radii) * np.cos(3 * angles)).flatten() - - # Create the Triangulation; no triangles so Delaunay triangulation created. - triang = tri.Triangulation(x, y) - - # Mask off unwanted triangles. - triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1), - y[triang.triangles].mean(axis=1)) - < min_radius) - - fig1, ax1 = plt.subplots() - ax1.set_aspect('equal') - tpc = ax1.tripcolor(triang, z, shading='flat') - fig1.colorbar(tpc) - ax1.set_title('tripcolor of Delaunay triangulation, flat shading') - - display(fig1) + # Sample data + data = { + 'Year': [2010, 2011, 2012, 2013, 2014], + 'Sales': [1000, 1500, 1800, 2000, 2100] + } + + # Create DataFrame + df = pd.DataFrame(data) + + # Plotting using Pandas and Matplotlib + df.plot(x='Year', y='Sales', kind='line', marker='o', color='skyblue') + plt.title('Sales Over Years') + plt.xlabel('Year') + plt.ylabel('Sales ($)') + plt.grid(True) + display(plt) .. raw:: html :file: ./_static/component.html \ No newline at end of file From ca9e48971055ed05f5fe0c565f06e787c21e9072 Mon Sep 17 00:00:00 2001 From: Aayush Badoni Date: Sun, 31 Mar 2024 06:11:17 +0530 Subject: [PATCH 5/5] removed unnecessary files --- build_info | 1 - 1 file changed, 1 deletion(-) delete mode 100644 build_info diff --git a/build_info b/build_info deleted file mode 100644 index 1d0fe84223..0000000000 --- a/build_info +++ /dev/null @@ -1 +0,0 @@ -unknown-0-0