# `gradio_logsview`
Visualize logs in your Gradio app
## Installation
```bash
pip install gradio_logsview
```
## Usage
```python
import logging
import random
import time
import gradio as gr
from gradio_logsview import LogsView
def random_values(failing: bool = False):
for i in range(10):
logging.log(
random.choice(
[ # Random levels
logging.INFO,
logging.DEBUG,
logging.WARNING,
logging.ERROR,
logging.CRITICAL,
]
),
f"Value {i+1}", # Random values
)
time.sleep(random.uniform(0, 1))
if failing and i == 5:
raise ValueError("Failing!!")
def fn_process_success():
yield from LogsView.run_process(["python", "-u", "demo/script.py"])
def fn_process_failing():
yield from LogsView.run_process(["python", "-u", "demo/script.py", "--failing"])
def fn_thread_success():
yield from LogsView.run_thread(random_values, log_level=logging.INFO, failing=False)
def fn_thread_failing():
yield from LogsView.run_thread(random_values, log_level=logging.INFO, failing=True)
markdown_top = """
# LogsView Demo
This demo shows how to use the `LogsView` component to display logs from a process or a thread in real-time.
Click on any button to launch a process or a thread and see the logs displayed in real-time.
In the thread example, logs are generated randomly with different log levels.
In the process example, logs are generated by a Python script but any command can be executed.
"""
markdown_bottom = """
## How to run in a thread?
With `LogsView.run_thread`, you can run a function in a separate thread and capture logs in real-time.
You can configure which logs to capture (log level and logger name).
```py
from gradio_logsview import LogsView
def fn_thread():
# Run `my_function` in a separate thread
# All logs above `INFO` level will be captured and displayed in real-time.
yield from LogsView.run_thread(my_function, log_level=logging.INFO, arg1="value1")
with gr.Blocks() as demo:
logs = LogsView()
btn = gr.Button("Run thread")
btn.click(fn_thread, outputs=logs)
```
## How to run in a process?
With `LogsView.run_process`, you can run a command in a separate process and capture logs from the process in real-time.
```py
from gradio_logsview import LogsView
def fn_process():
# Run a process and capture all logs from the process
yield from LogsView.run_process(
cmd=[mergekit-yaml", "config.yaml", "merge", "--copy-", "--cuda", "--low-cpu-memory"]
)
with gr.Blocks() as demo:
logs = LogsView()
btn = gr.Button("Run process")
btn.click(fn_process, outputs=logs)
```
"""
with gr.Blocks() as demo:
gr.Markdown(markdown_top)
with gr.Row():
btn_thread_success = gr.Button("Run thread (success)")
btn_thread_failing = gr.Button("Run thread (failing)")
with gr.Row():
btn_process_success = gr.Button("Run process (success)")
btn_process_failing = gr.Button("Run process (failing)")
logs = LogsView()
gr.Markdown(markdown_bottom)
btn_thread_failing.click(fn_thread_failing, outputs=logs)
btn_thread_success.click(fn_thread_success, outputs=logs)
btn_process_failing.click(fn_process_failing, outputs=logs)
btn_process_success.click(fn_process_success, outputs=logs)
if __name__ == "__main__":
demo.launch()
```
## `LogsView`
### Initialization
name | type | default | description |
---|---|---|---|
value |
```python str | Callable | tuple[str] | None ``` | None |
Default value to show in the code editor. If callable, the function will be called whenever the app loads to set the initial value of the component. |
every |
```python float | None ``` | None |
If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute. |
lines |
```python int ``` | 5 |
None |
label |
```python str | None ``` | None |
The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to. |
show_label |
```python bool | None ``` | None |
if True, will display label. |
container |
```python bool ``` | True |
If True, will place the component in a container - providing some extra padding around the border. |
scale |
```python int | None ``` | None |
relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True. |
min_width |
```python int ``` | 160 |
minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first. |
visible |
```python bool ``` | True |
If False, component will be hidden. |
elem_id |
```python str | None ``` | None |
An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles. |
elem_classes |
```python list[str] | str | None ``` | None |
An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles. |
render |
```python bool ``` | True |
If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later. |