pip_list_tab.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import gradio as gr
  2. import importlib.metadata
  3. def pip_list_tab():
  4. with gr.Tab("Installed Packages"):
  5. pip_list = gr.Markdown("Press Refresh to load the list")
  6. gr.Button("Refresh").click(fn=refresh_pip_list, outputs=pip_list)
  7. gr.Button("API_GET_PIP_LIST", visible=False).click(
  8. fn=get_pip_list,
  9. outputs=[gr.JSON(None, visible=False)],
  10. api_name="get_pip_list",
  11. )
  12. def get_pip_list():
  13. packages = [
  14. {
  15. "name": x.name,
  16. "version": x.version,
  17. }
  18. for x in importlib.metadata.distributions()
  19. ]
  20. return packages
  21. def render_pip_list(pip_list):
  22. if isinstance(pip_list, list):
  23. return "\n\n".join([f"{x['name']}=={x['version']}" for x in pip_list])
  24. else:
  25. return pip_list
  26. def refresh_pip_list():
  27. return render_pip_list(get_pip_list())
  28. if __name__ == "__main__":
  29. if "demo" in locals():
  30. demo.close() # type: ignore
  31. with gr.Blocks() as demo:
  32. pip_list_tab()
  33. demo.launch()