Drawing a line chart in PyScript using Matplotlib
import asyncio
import panel as pn
import pandas as pd
from panel.io.pyodide import show
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
df = pd.DataFrame()
def process_file(event):
global df
if fileInput.value is not None:
df = pd.read_csv(io.BytesIO(fileInput.value))
# columns = ['Population_Total', 'Australia', 'Brazil', 'Canada', 'Indonesia', 'Italy', 'United Kingdom']
# df.drop(columns, axis=1, inplace=True)
table.value = df
document.getElementById('table').style.display = 'block'
def draw_chart(event):
plt.style.use('dark_background')
fig, ax = plt.subplots()
countries_to_look_at = ['China', 'India', 'Japan', 'United States']
for country in df:
console.log(country)
if country in countries_to_look_at:
ax.plot(df.SurveyYear, df[country], label=country, marker='.')
ax.set(xlabel='Year', ylabel='Population', title='Population Line Graph')
ax.legend()
ax.grid()
pyscript.write('plot', fig)
fileInput = pn.widgets.FileInput(accept='.csv')
uploadButton = pn.widgets.Button(name='Upload', button_type = 'primary')
table = pn.widgets.Tabulator(pagination='remote', page_size=10)
# hide pagination 'First, Prev [1][2][3] Next, Last'
document.getElementById('table').style.display = 'none'
drawButton = pn.widgets.Button(name='Draw', button_type = 'primary')
uploadButton.on_click(process_file)
drawButton.on_click(draw_chart)
await show(fileInput, 'fileinput')
await show(uploadButton, 'upload')
await show(table, 'table')
await show(drawButton, 'draw')