Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: use global singleton UI object for printing #5653

Merged
merged 1 commit into from
Mar 24, 2021

Conversation

skshetry
Copy link
Member

@skshetry skshetry commented Mar 19, 2021

I understand this is a bit controversial, but it seems that using this ui object globally is the best thing to do.
DVC tries to provide a sane API as well as CLI. Extending api to accomodate ui object (which is for cli) does not seem like a good thing, as it extends our API surface and makes it ugly. We'd also need to pass them to the depth of our APIs.

Similarly, we cannot just use ui object in CLIs, as our cli commands are usually just wrapper to the Repo apis. So, even if we try hard to separate them and isolate it inside the CLI, it'd be quite difficult.

Looking at it, and the purpose that ui object serves, it seems to me that it is a global singleton object anyway (an instance in a single session of CLI). And, we use loggers in a similar way, so I find it fitting as per our needs, without adding any extra efforts.

Thank you for the contribution - we'll try to review it as soon as possible. 🙏

@skshetry skshetry added ui user interface / interaction discussion requires active participation to reach a conclusion labels Mar 19, 2021
@skshetry skshetry self-assigned this Mar 19, 2021
@@ -152,9 +152,11 @@ def table(self, header, rows, markdown: bool = False):
self.write(ret)


if __name__ == "__main__":
ui = Console()
ui = Console()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it should be console now?

@skshetry skshetry changed the title ui: use global UI object for printing ui: use global singleton UI object for printing Mar 19, 2021
file: TextIO = None,
flush: bool = False,
) -> None:
if self.disabled:
if not self._enabled:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: How important is thread-safety for us while printing? logger is thread safe, but this is not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, ultimately, we are intending to use ui.write everywhere we use logger now? If so, I would say thread safety is pretty important as we already have some parts of code that utilize ThreadPoolExecutor and log something down the line (for example, _upload_plans.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, @pared. Will come to it as we start using it in multithreaded applications.

@efiop
Copy link
Contributor

efiop commented Mar 19, 2021

@skshetry Could you clarify how we would change verbosity or in general tweak ui in cases like erepo?

@skshetry
Copy link
Member Author

skshetry commented Mar 19, 2021

Could you clarify how we would change verbosity

Not implemented, but verbosity could be changed in a similar way. Probably, changing the API of enable to configure.

ui.configure(level="success", enable=True)

But, I think we should not do that, as I am thinking ui.write to be always visible (except on --quiet). Verbosity will display logging information as well.

or in general tweak ui in cases like erepo?

I had implemented this before, but removed it to keep API simple. I am thinking of using contextvars here (requires compat package in 3.6, in stdlib since 3.7). It'll look something like following:

with ui.enabled():
  ui.write("visible")
  with ui.disabled():
    ui.write("hidden")
    with ui.enabled():
      ui.write(" visible again")
    ui.write(" hidden again")

This should be thread and async safe.

@efiop
Copy link
Contributor

efiop commented Mar 24, 2021

@skshetry Sorry for the delay. Looks pretty good. 👍

@skshetry
Copy link
Member Author

Okay, merging it for now.

@skshetry skshetry merged commit 8b6d655 into iterative:master Mar 24, 2021
@skshetry skshetry deleted the global-ui branch March 24, 2021 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion requires active participation to reach a conclusion ui user interface / interaction
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants