Add display impl, rm outputManage, print and console.log default to browser console (#749)

* Add display impl, remove outputManage, print and console.log defaults to terminal

* Fixing tests

* Lint

* Erase unecessary code, add cuter CSS formating for errors, fix problems around REPL output

* Add fix to repl2 and lint

* lint

* Allow for list of display, fix elif to else

* Add better global option

* test work

* xfails

* (antocuni, mariana): let's try to start again with TDD methodology: write the minimum test and code for a simple display()

* (antocuni, mariana): this test works out of the box

* WIP: this test is broken, mariana is going to fix it

* add a failing test

* Add ability to deal with targets

* Add append arg and append tests

* Add multiple values to display

* Small adjustments to tests. I noticed I wasn;t running all at some point

* add display test

* Add console tests

* Add async tests

* Fix repl tests

* Fixing merging issues

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Address antocuni's review

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fixing more tests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* linting

* Improve repl tests

* Change my test so codespell is hapy with it

* Test: change test_runtime_config to use json instead of toml to see if stops failing on CI

* kill this file: it is a merge artifact since it was renamed into test_py_config.py on the main branch

* Change test execution order and add async tests to async test file

Co-authored-by: Antonio Cuni <anto.cuni@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Mariana Meireles
2022-10-17 16:28:40 +02:00
committed by GitHub
parent beb3aa1574
commit 1587273868
18 changed files with 459 additions and 180 deletions

View File

@@ -1,7 +1,6 @@
import asyncio
import base64
import io
import sys
import time
from textwrap import dedent
@@ -124,6 +123,33 @@ class PyScript:
)
def set_current_display_target(target_id):
get_current_display_target._id = target_id
def get_current_display_target():
return get_current_display_target._id
get_current_display_target._id = None
def display(*values, target=None, append=True):
default_target = get_current_display_target()
if default_target is None and target is None:
raise Exception(
"Implicit target not allowed here. Please use display(..., target=...)"
)
if target is not None:
for v in values:
Element(target).write(v, append=append)
else:
for v in values:
Element(default_target).write(v, append=append)
class Element:
def __init__(self, element_id, element=None):
self._id = element_id
@@ -365,59 +391,4 @@ class PyListTemplate:
pass
class OutputCtxManager:
def __init__(self, out=None, output_to_console=True, append=True):
self._out = out
self._prev = out
self.output_to_console = output_to_console
self._append = append
def change(self, out=None, output_to_console=True, append=True):
self._prev = self._out
self._out = out
self.output_to_console = output_to_console
self._append = append
def revert(self):
self._out = self._prev
def write(self, value):
if self._out:
Element(self._out).write(value, self._append)
if self.output_to_console:
console.info(value)
class OutputManager:
def __init__(self, out=None, err=None, output_to_console=True, append=True):
sys.stdout = self._out_manager = OutputCtxManager(
out=out, output_to_console=output_to_console, append=append
)
sys.stderr = self._err_manager = OutputCtxManager(
out=err, output_to_console=output_to_console, append=append
)
self.output_to_console = output_to_console
self._append = append
def change(self, out=None, err=None, output_to_console=True, append=True):
self._out_manager.change(
out=out, output_to_console=output_to_console, append=append
)
sys.stdout = self._out_manager
self._err_manager.change(
out=err, output_to_console=output_to_console, append=append
)
sys.stderr = self._err_manager
self.output_to_console = output_to_console
self._append = append
def revert(self):
self._out_manager.revert()
self._err_manager.revert()
sys.stdout = self._out_manager
sys.stderr = self._err_manager
pyscript = PyScript()
output_manager = OutputManager()