Hi Zweb1,

THis is a really good question,

We use Python built-in exec (
https://docs.python.org/3/library/functions.html#exec), which allow you to
execute some code and pass a namespace in which to execute it.
You then put it in a while loop, and wait for the user to send some code.
Exec send the result back.
Here is a 5 lines REPL (read eval print loop) as  example on how to
evaluate user code interactively.

$ cat repl.py

File: repl.py
   1   │
   2   │ namespace = dict()
   3   │
   4   │
   5   │ while True:
   6   │     code = input('>>> ')
   7   │     exec(code, namespace)

 $ python repl.py
>>> a = 1
>>> print(a)
1
>>> print(a+1)
2
>>>

You you were to look at namespace, you would see that after a = 1, we have
namespace['a'] ==  1, so the namespace dict hold all our variables between
exec.

This +/- 40 000 lines of code give you IPython, there is a lot not handled
there, but if you take the above, handle exceptions, multiple
statements.... etc and forward stdin and out over the network you have a
rough prototype of what Jupyter is.

Does that answer your question?
-- 
Matthias

On Sat, 20 Jul 2019 at 19:08, zweb1 <[email protected]> wrote:

> How does jupyter kernel maintain context of all the previous cell
> executions in a session? How is it implemented?
>
> example:
>
> cell [1]: import pandas
>
> cell[2]: x = 7
>
> cell[3] print(x)
>
> 3
>
> When executing cell 3, Jupyter Kernel has x from cell 2 and pandas import
> from cell 1 execution. How is it implemented in the kernel?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Project Jupyter" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jupyter/2312ef59-7f49-4ae4-927b-db278d69e35e%40googlegroups.com
> <https://groups.google.com/d/msgid/jupyter/2312ef59-7f49-4ae4-927b-db278d69e35e%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jupyter/CANJQusXiG2fsp5gK-aWOHmahKD19e74jr6ChCgnM-do3rqyM%3DA%40mail.gmail.com.

Reply via email to