View Source

# INITIALIZATION from flask import Flask, request import pyvibe as pv import pycob as cob import plotly.express as px app = Flask(__name__) pypi_projects_by_month = cob.fetch_pickle('pypi_projects_by_month.pkl') # PAGE FUNCTIONS @app.route('/') def home() -> str: page = pv.Page("PyPi Analytics") page.add_header("PyPi Analytics") page.add_text("PyPi analytics enables Python engineers to identify usage trends with Python packages as an input for picking the right package") page.add_link("See the source code for this app", "/view_source") page.add_text("") top_projects = pypi_projects_by_month.groupby('pypi_project').sum().sort_values('avg_downloads_per_day', ascending=False).reset_index().head(50) action_buttons = [ cob.Rowaction(label="Analytics", url="/project_detail?project_name={pypi_project}", open_in_new_window=True), cob.Rowaction(label="Project", url="https://pypi.org/project/{pypi_project}", open_in_new_window=True), ] with page.add_card() as card: card.add_header("Analyze a Project") with card.add_form(action="/project_detail") as form: form.add_formtext(name="project_name", label="Project Name", placeholder="Enter a project name") form.add_formsubmit("Analyze") page.add_text("") page.add_header("Top 50 Projects by Downloads", size=4) page.add_pandastable(top_projects, action_buttons=action_buttons) return page.to_html() @app.route('/project_detail') def project_detail() -> str: page = pv.Page("PyPi Analytics") project_name = request.args['project_name'] if 'compare_to' in request.args: compare_to = request.args['compare_to'] subtitle = request.args['compare_to'] else: compare_to = None subtitle = None page.add_header(f"PyPi Analytics for {project_name}") if not subtitle: with page.add_form(action="/project_detail") as form: form.add_formhidden(name="project_name", value=project_name) form.add_formhidden(name="compare_to", value=compare_to if compare_to else "") form.add_formtext(name="subtitle", label="Subtitle", placeholder="Enter a subtitle") form.add_formsubmit("Update Subtitle") else: page.add_header(f"{subtitle}", size=4) if compare_to: project_detail = pypi_projects_by_month[pypi_projects_by_month['pypi_project'].isin([project_name, compare_to])] else: project_detail = pypi_projects_by_month[pypi_projects_by_month['pypi_project'] == project_name] fig = px.line(project_detail.sort_values(["month", "pypi_project"]), x="month", y="avg_downloads_per_day", line_group="pypi_project", color="pypi_project") page.add_plotlyfigure(fig) if not compare_to: with page.add_card() as card: with card.add_form(action="/project_detail") as form: form.add_formhidden(name="project_name", value=project_name) form.add_formtext(label="Compare To", name="compare_to", placeholder="Enter a project name") form.add_formsubmit("Analyze") return page.to_html() @app.route('/view_source') def view_source() -> str: page = pv.Page("View Source") page.add_header("View Source") # Read the source code with open(__file__, 'r') as f: source = f.read() # Add the source code to the page page.add_codeeditor(source, language="python") return page.to_html() # RUN APP if __name__ == '__main__': app.run(debug=True)