Jump to content

GSoC/2025/StatusReports/ZhengJiahong

From KDE Community Wiki

Python virtual environments in Cantor

Cantor is a powerful application that lets users use their favorite mathematical programming language from within a nice worksheet interface. It supports several backends, such as Python, Julia, R, Lua and so on.

However, its Python integration currently exhibits a critical functional gap: the absence of virtual environment management. Notably, mainstream IDEs like PyCharm and Spyder have long provided built-in virtual environment switching capabilities – a fundamental feature that significantly improves UX. At the same time, many users have hoped that Cantor could integrate Python virtual environments, like (Bug 457681) .This request the importance of implementing similar virtual environment management functionality in Cantor's Python backend to enhance its competitiveness and user experience.

Mentors

Alexander Semke

Israel Galadima

Work report

Week 1

1. Use Py_SetPythonHome to switch virtual environments (not feasible in practice)

2. Understand that the virtual environments created by pyenv and virtualenv are lightweight virtual environments (some libraries still rely on the system's own python libraries)

Week 2-4

(Because I need to finish the proposal of my graduation project during this period, I asked for leave from my mentors.)

Week 5

1. Implement runtime switching of virtual environments (virtualenv, venv) for Python 3.8 and above (via PyConfig)

2. Discussed other implementation details (only supports environment switching of the same major version, automatically scans virtual environments under user paths, and obtains Python environments through cmake dynamic links)

Week 6

1. Implement virtual environment switching for Python 3.5 and above (virtual environments below 3.5 are not yet complete)

2. Automatically obtain the virtual environment in the user directory

3. Provide users with an interface for importing virtual environments

4. Link the Python environment in the system through cmake

Week 7

Confirmed with mt the details of user interaction in the Cantor front-end:

(1) Users switch virtual environments through the General Tab in Python settings

(2) Switch virtual environments through a drop-down box (similar to VsCode)

(3) The drop-down box supports adding virtual environments based on the interpreter path and scanning virtual environments based on folders

(4) The drop-down box automatically scans the environment list and removes invalid environments each time the user opens it

(5) Users can delete environments (only delete in the environment list)

(6) After switching environments, the environment will be broadcast to all worksheets (that is, all worksheets share the same virtual environment)

(7) After switching environments, the previous content of the worksheet will be invalid (from the user's perspective, the cells will be grayed out and unclickable)

Week 8

1. Continued to refine PythonServer's virtual environment switching details and improved the environment list added in PyConfig.

2. Improved the code for interaction between PythonServer and PythonSession

Week 9

1. Add a drop-down box in the General tab to allow users to switch virtual environments.

2. Improve the code for interacting with PythonServer and PythonSession.

Week 10

1. Add more functionality to the drop-down list. The first two options in the drop-down list now provide users with two fixed functions: adding virtual environments based on the interpreter path and scanning for virtual environments based on a folder.

2. Each time a user opens the drop-down list, the list of environments will be automatically scanned and invalid environments will be removed.

3. A delete button is now available next to the drop-down list, allowing users to delete environments from within the list.

Week 11

1. Switching environments will broadcast the changes to all worksheets.

2. After switching environments, the previous worksheet contents will be invalidated.

3. PythonServer supports switching between Conda environments.

Future Plans

1. The design of user interaction in the front end needs to be improved

2. Pythonserver needs more exception handling mechanisms

Links

Blogs on Planet KDE

Python Virtual Environment Switching with CPython

Runtime Python Virtual Environment Switching based on Cantor

Related Merge Requests

https://invent.kde.org/education/cantor/-/merge_requests/119

Related Issues

Contact

[[email protected] [email protected]]

@zjh777:matrix.org







GSoC Requirements

It must be easy to identify the work you have done. (i.e. the changes you made or new code.)

When someone goes to the provided URL it should be clear what work you did without requiring them to do significant additional digging.

It should be in a stable location. The URL cannot be changed after submission.

Someone else should be able to use the content at (or referenced from) the target of the link to extend your work.

If your work is 100% complete, they should be able to use it.

If your work is not 100% complete, it should be clear what's left to do.

KDE Requirements

A good status report will include:

  1. Design documents the student created
  2. Short video of the work, or screenshots as appropriate
  3. Link to a list of commits (example)
  4. Link to blog posts (on KDE Planet, at least monthly)