PyScript is awesome, it just takes a few seconds to load...
To provide the power of Python on your browser, this technology needs to load a special virtual machine and all necessary Python modules
So, sit back and relax. We are loading the modules and generating some random variables as a test.
You can start interacting with the page when you see a histogram of those random variates displayed below.
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.
import matplotlib.pyplot as plt
import numpy as np
integers = np.random.randint(0,100,100)
fig1,ax1 = plt.subplots(1,1,dpi=150,figsize=(5,2))
ax1.hist(integers,bins=20,edgecolor='k',color='blue')
ax1.set_title("Histogram of random integers",fontsize=11)
pyscript.write('test-plot',fig1)
Pandas - random dataframe
Let's generate a Pandas DataFrame with some random nunbers (drawn from the ubiquitous Gaussian Normal distribution).
The pretty formatting of the table is done, of course, using CSS. Check the style section. The Pandas pd.to_html()
function has a classes argument that accepts a CSS class or a list of classes. We utilize that formatting this table.
Interested in the statistics of this dataset?
This code is non-trivial as the randomized generation of the DataFrame with the button above makes it imperative that we read the exact content shown above.
So, we use the document.getElementbyId() function to accomplish this and read back that HTML into Pandas using the pd.read_html() function.
Download file and analyze
Now, we will download a CSV file from an externally hosted URL (a GitHub repo) and show its contents and statistics.
First 5 records of the downloaded file
Statistics of the downloaded file
import pandas as pd, numpy as np
from js import document
from pyodide.http import open_url
def random_table(n_cols=5,n_rows=20,*ags, **kws):
cols = ['Col-'+str(i) for i in range(1,n_cols+1)]
idx = ['Row-'+str(i) for i in range(1,n_rows+1)]
data = np.round(np.random.normal(size=(n_rows,n_cols)),2)
df = pd.DataFrame(data,columns=cols,index=idx)
return df
def show_table(*ags, **kws):
out = Element('pandas-output')
random_df = random_table()
out.write(random_df.to_html(classes='pd-table'))
def show_stats(*ags, **kws):
out = Element('pandas-ops')
input_div = document.getElementById("pandas-output")
html_data = input_div.children[0].outerHTML
df = pd.read_html(html_data,flavor='lxml')[0]
stats_df = df.describe()
out.write(stats_df.to_html(classes='pd-table'))
def download_analyze(*ags, **kws):
url=document.getElementById('file-url').value
try:
df = pd.read_csv(open_url(url))
pyscript.write('url-file-head',df.head(5).to_html(classes='pd-table'))
pyscript.write('url-file-ops',df.describe().to_html(classes='pd-table'))
except:
pyscript.write('url-file-head',"Could not access the file")