gperdrizet commited on
Commit
9e8809b
·
verified ·
1 Parent(s): c692c0a

Added server log output to UI.

Browse files
Files changed (2) hide show
  1. functions/gradio_functions.py +62 -0
  2. rss_server.py +20 -6
functions/gradio_functions.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''Collection of helper functions for Gradio UI and interface.'''
2
+
3
+ import os
4
+ import re
5
+ import logging
6
+ from pathlib import Path
7
+ from logging.handlers import RotatingFileHandler
8
+
9
+
10
+ def configure_root_logger() -> None:
11
+ '''Configures root logger for project-wide logging.'''
12
+
13
+ # Make sure log directory exists
14
+ Path('logs').mkdir(parents=True, exist_ok=True)
15
+
16
+ # Clear old logs if present
17
+ delete_old_logs('logs', 'rss_server')
18
+
19
+ # Set up the root logger so we catch logs from
20
+ logging.basicConfig(
21
+ handlers=[RotatingFileHandler(
22
+ 'logs/rss_server.log',
23
+ maxBytes=100000,
24
+ backupCount=10,
25
+ mode='w'
26
+ )],
27
+ level=logging.INFO,
28
+ format='%(levelname)s - %(name)s - %(message)s'
29
+ )
30
+
31
+
32
+ def update_log(n: int = 10):
33
+ '''Gets updated logging output from disk to display to user.
34
+
35
+ Args:
36
+ n: number of most recent lines of log output to display
37
+
38
+ Returns:
39
+ Logging output as string
40
+ '''
41
+
42
+ with open('logs/rss_server.log', 'r', encoding='utf-8') as log_file:
43
+ lines = log_file.readlines()
44
+
45
+ return ''.join(lines[-n:])
46
+
47
+
48
+ def delete_old_logs(directory:str, basename:str) -> None:
49
+ '''Deletes old log files from previous optimization sessions, if present.
50
+
51
+ Args:
52
+ directory: path to log file directory as string
53
+ basename: log file base name as string
54
+
55
+ Returns:
56
+ None
57
+ '''
58
+
59
+ for filename in os.listdir(directory):
60
+ file_path = os.path.join(directory, filename)
61
+ if re.search(basename, filename):
62
+ os.remove(file_path)
rss_server.py CHANGED
@@ -6,14 +6,18 @@ from logging.handlers import RotatingFileHandler
6
 
7
  import gradio as gr
8
  import assets.html as html
9
- from functions import tools as tool_funcs
 
10
 
 
 
11
  # Make sure log directory exists
12
  Path('logs').mkdir(parents=True, exist_ok=True)
13
 
14
- # Set-up logger
15
- logger = logging.getLogger()
16
 
 
17
  logging.basicConfig(
18
  handlers=[RotatingFileHandler(
19
  'logs/rss_server.log',
@@ -30,10 +34,20 @@ logger = logging.getLogger(__name__)
30
 
31
  with gr.Blocks() as demo:
32
 
33
- with gr.Row():
34
- gr.HTML(html.TITLE)
 
 
 
 
 
 
 
 
 
 
35
 
36
- gr.Markdown(html.DESCRIPTION)
37
  website_url = gr.Textbox('hackernews.com', label='Website')
38
  output = gr.Textbox(label='RSS entries', lines=10)
39
  submit_button = gr.Button('Submit')
 
6
 
7
  import gradio as gr
8
  import assets.html as html
9
+ import functions.tools as tool_funcs
10
+ import functions.gradio_functions as gradio_funcs
11
 
12
+
13
+ # Set-up logging
14
  # Make sure log directory exists
15
  Path('logs').mkdir(parents=True, exist_ok=True)
16
 
17
+ # Clear old logs if present
18
+ gradio_funcs.delete_old_logs('logs', 'rss_server')
19
 
20
+ # Set up the root logger so we catch logs from
21
  logging.basicConfig(
22
  handlers=[RotatingFileHandler(
23
  'logs/rss_server.log',
 
34
 
35
  with gr.Blocks() as demo:
36
 
37
+ # Page text
38
+ gr.HTML(html.TITLE)
39
+ gr.HTML(html.DESCRIPTION)
40
+
41
+ # Log output
42
+ dialog_output = gr.Textbox(label='Server logs', lines=10, max_lines=100)
43
+ timer = gr.Timer(0.5, active=True)
44
+
45
+ timer.tick( # pylint: disable=no-member
46
+ lambda: gradio_funcs.update_log(), # pylint: disable=unnecessary-lambda
47
+ outputs=dialog_output
48
+ )
49
 
50
+ # Get feed tool
51
  website_url = gr.Textbox('hackernews.com', label='Website')
52
  output = gr.Textbox(label='RSS entries', lines=10)
53
  submit_button = gr.Button('Submit')