A method for executing a block of python code within a context that supports variable intercommunication

CN122816693APending Publication Date: 2026-09-25ZHIHUITONG (CHONGQING) DIGITAL INTELLIGENCE TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202610958384.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-06-30
Publication Date
2026-09-25

AI Technical Summary

Technical Problem

这种方案存在缺点:包括需要额外服务器资源支持、代码执行延迟高、不符合教学场景、用户体验差;且存在代码泄漏和安全风险,无法实现实时交互

Benefits of technology

[0013]综上所述,采用上述的方法能够实现同一文档中的多个Python代码块可共享变量、函数和导入的模块,能够实现编辑与执行的无缝切换,能够实现统一的执行结果展示机制,能够实现浏览器端本地执行,能够支持代码执行状态追踪。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122816693A_ABST
    Figure CN122816693A_ABST
Patent Text Reader

Abstract

The present application relates to the technical field of internet data processing, in particular to a context-in Python code block execution method supporting variable intercommunication, comprising the following steps: S1, configuring a Python code block type; S2, initializing a shared Pyodide runtime context; S3, a code execution extension process; S4, implementation of a variable intercommunication mechanism; S5, output result display; S6, execution state visualization; in S1, in the code block extension of a rich text editor, a special pyrun language type is defined to identify executable Python code blocks; in S2, a Pyodide runtime is loaded asynchronously through a configuration item setCodeContext, and the runtime remains a singleton in the document life cycle.The present application has the characteristics of being able to realize that multiple Python code blocks in the same document can share variables, functions and imported modules, being able to realize seamless switching between editing and execution, being able to realize a unified execution result display mechanism, being able to realize local execution on the browser side and being able to support code execution state tracking.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of Internet data processing technology, and in particular to a method for executing Python code blocks within a context that supports variable interoperability. Background Technology

[0002] With the digital transformation of education and the growing demand for interactive documents, more and more rich text editors need to support the display and execution of code blocks. Current technologies mainly include: traditional code block display methods: rich text editors only support syntax highlighting and static display of code blocks, and cannot directly execute code in the browser.

[0003] Server-side execution solution: Some platforms implement code execution by sending the code to a server for execution and then returning the result. This solution has disadvantages, including the need for additional server resources, high code execution latency, unsuitability for teaching scenarios, poor user experience, code leakage and security risks, and inability to achieve real-time interaction.

[0004] Independent Sandbox Solution: Some online education platforms use iframes or Web Workers to create independent code execution environments, but each code block has its own execution context. Variables and states cannot be shared between multiple code blocks, resulting in: users being unable to pass data between multiple code segments, lack of support for step-by-step execution and progressive programming teaching, and inability to achieve an interactive programming experience similar to Jupyter Notebook.

[0005] Limitations of existing WebAssembly solutions: Although WebAssembly technologies such as pyodide can run Python in a browser, current applications are usually standalone code runners that fail to integrate deeply with rich text editors. They lack: linkage between editor nodes and code execution status, fusion of code blocks and document context, visualization of execution results, and unified management of the editor.

[0006] In summary, the main problems with existing technologies include: 1. Variable isolation: Each code block executes independently, making it impossible to share variable and function definitions. Users need to repeatedly define the same variables, reducing efficiency. 2. Segmented execution context: There is a lack of state transfer mechanisms between code blocks, making continuous calculation processes impossible. 3. Limited result display: Execution results are usually displayed in plain text format, without supporting multimedia output such as icons and formulas. 4. Response latency: Server-based execution solutions suffer from high network latency, impacting user experience. Summary of the Invention

[0007] In view of the above-mentioned deficiencies of the prior art, the technical problem to be solved by the present invention is how to provide a method for executing Python code blocks within a context that supports variable sharing, allows multiple Python code blocks in the same document to share variables, functions, and imported modules, enables seamless switching between editing and execution, provides a unified execution result display mechanism, enables local execution on the browser side, and supports code execution status tracking.

[0008] To achieve the above objectives, this invention provides a method for executing Python code blocks within a context that supports variable interoperability, comprising the following steps: S1, configuring the Python code block type; S2, initializing the shared Pyodide runtime context; S3, extending the code execution flow; S4, implementing the variable interoperability mechanism; S5, displaying the output results; S6, visualizing the execution status; characterized in that: in S1, a special pyrun language type is defined in the code block extension of the rich text editor to identify executable Python code blocks; in S2, the Pyodide runtime is asynchronously loaded through the configuration item setCodeContext, and this runtime remains a singleton throughout the document's lifecycle.

[0009] As an optimization, S5 adds an output area below the code block to display the execution results.

[0010] As an optimization, in S6, the execution status of a code block is displayed through a status display.

[0011] As an optimization, in S3, when the user clicks the run button of the code block, the following process is executed: condition preflight, environment initialization, obtaining the shared runtime instance, output target binding, configuration stream capture callback, dependency package preloading, script execution, and state update.

[0012] As an optimization, in S4, all code blocks share the same Pyodide instance, and variables are naturally interchangeable.

[0013] In summary, the above methods enable multiple Python code blocks within the same document to share variables, functions, and imported modules; achieve seamless switching between editing and execution; provide a unified execution result display mechanism; enable local execution on the browser side; and support code execution status tracking. Attached Figure Description

[0014] Figure 1 This is a system architecture diagram in a specific embodiment of the present invention.

[0015] Figure 2 This is a flowchart illustrating the code execution process in a specific embodiment of the present invention.

[0016] Figure 3 This is a schematic diagram illustrating variable communication in a specific embodiment of the present invention.

[0017] Figure 4 This is a schematic diagram of the UI interface of the code block in a specific embodiment of the present invention.

[0018] Figure 5 This is a schematic diagram of a code execution indicator in a specific embodiment of the present invention. Detailed Implementation

[0019] The present invention will be further described below with reference to the accompanying drawings and embodiments. It should be noted that in the description of the present invention, terms such as "upper," "lower," "left," "right," "inner," and "outer" indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings. These terms are used only for the convenience of describing the present invention and for simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific manner. Therefore, they should not be construed as limitations on the present invention. Terms such as "first," "second," and "third" are used for descriptive purposes only and should not be construed as indicating or implying relative importance. Example

[0020] I. For example Figures 1 to 5 The diagram shows the system architecture for implementing in-context Python code block execution methods that support variable interoperability: it includes a rich text editor layer, a code execution management layer, a Pyodide shared runtime context, and a WebAssembly layer. The rich text editor layer includes code block nodes 1, 2, to N of type pyrun; the code execution management layer includes a runtime control module, an output management module, and a state tracking module; the Pyodide shared runtime context includes a global namespace, an imported module cache, and a standard output stream.

[0021] II. Specific Implementation Steps: Step S1: Configure Python code block types. In the rich text editor's code block extension, define a special pyrun type to identify executable Python code blocks, as shown in Table 1 below.

[0022] Step S2: Initialize the shared Pyodide runtime context The Pyodide runtime is loaded asynchronously via the setCodeContext configuration option, and this runtime remains a singleton throughout the document's lifecycle. As shown in Table 2 below,

[0023] Step 3: Code Execution Extension Process When the user clicks the run button on the code block, the following process is executed: (Pseudocode form, as shown in Table 3 below)

[0024] Step S4: Implementation of the variable communication mechanism. Since all code blocks share the same Pyodide instance, variables are naturally interchangeable, as shown in Table 4 below.

[0025] Step S5: Output results display. Set an output area below the code block to display the execution results: as shown in Table 5 below, the effect is as attached. Figure 4 As shown;

[0026] Step S6: Visualize the execution status; The execution status of a code block is displayed using a status display, as shown in the attached image. Figure 5 As shown.

[0027] III. Key Data Structures: Code block node attributes: as shown in Table 6 below.

[0028] Output record structure: as shown in Table 7 below.

[0029] Execution status: As shown in Table 8 below.

[0030] IV. Compared with the prior art, the present invention has the following significant advantages and technical effects: 1. Variable interoperability, supporting continuous programming: By sharing the pyodide runtime context, all Python code blocks in the same document share a global namespace, allowing users to pass variables and state between multiple code blocks, achieving an interactive programming experience similar to Jupyter Notebook.

[0031] Comparison of effects: 1) Existing technology: Each code block requires the repeated definition of variables, which cannot be reused; 2) This invention: Variables are defined once and are available globally. 2. Runs locally in the browser, with a fast response time: By using WebAssembly technology, Python code can be executed directly in the browser without server support, eliminating network latency and reducing code execution response time from seconds to milliseconds.

[0032] Comparison of effects: 1) Server-side execution plan: Response time 500ms-5s. 2) This invention: Response time 10ms-100ms.

[0033] 3. Deeply integrated editor for smooth operation: By making code execution a native capability of the editor nodes, users can write code in edit mode and execute it in preview mode without switching pages.

[0034] 4. Diverse output formats: It supports multiple output formats, including standard output, error messages, and Matplotlib charts, to meet the needs of various scenarios such as teaching and data visualization.

[0035] 5. Execution status is traceable: The status indicator visually displays the execution status of code blocks, allowing users to clearly perceive which code has been executed and which has not.

[0036] 6. High security: The code executes in a WebAssembly sandbox on the browser side, without accessing the user's local file system or sending the code to external servers, effectively protecting code privacy.

[0037] 7. Automatic dependency management: By using loadPackagesFromImports, the code's import statements are automatically analyzed and the necessary Python packages are loaded, lowering the barrier to entry for users.

[0038] The preferred embodiments of the present invention have been described in detail above. It should be understood that those skilled in the art can make numerous modifications and variations based on the concept of the present invention without creative effort. Therefore, all technical solutions that can be obtained by those skilled in the art based on the concept of the present invention through logical analysis, reasoning, or limited experimentation on the basis of existing technology should be within the scope of protection defined by the claims.

Claims

1. A method for executing Python code blocks within a context that supports variable interoperability, comprising the following steps: S1. Configure Python code block types; S2. Initialize the shared Pyodide runtime context; S3. Extend the code execution flow; S4. Implement the variable communication mechanism; S5. Display the output results; S6. Visualize the execution status; The key feature is that in S1, a special pyrun language type is defined in the code block extension of the rich text editor to identify executable Python code blocks; in S2, the Pyodide runtime is asynchronously loaded through the configuration item setCodeContext, and this runtime remains a singleton throughout the document's lifecycle.

2. The method for executing Python code blocks within a context that supports variable communication as described in claim 1, characterized in that: In S5, an output area is set below the code block to display the execution results.

3. The method for executing Python code blocks within a context that supports variable communication as described in claim 1, characterized in that: In S6, the execution status of a code block is displayed through a status display.

4. The method for executing Python code blocks within a context that supports variable communication as described in claim 1, characterized in that: In S3, when a user clicks the run button on a code block, the following process is executed: condition preflight, environment initialization, obtaining a shared runtime instance, output target binding, configuration stream capture callback, dependency preloading, script execution, and state update.

5. The method for executing Python code blocks within a context that supports variable communication as described in claim 1, characterized in that: In S4, all code blocks share the same Pyodide instance, and variables are naturally interchangeable.