davidlms commited on
Commit
6c78ba8
·
verified ·
1 Parent(s): dfee995

Update ipmentor/ui.py

Browse files
Files changed (1) hide show
  1. ipmentor/ui.py +50 -9
ipmentor/ui.py CHANGED
@@ -4,7 +4,12 @@ Gradio UI for IPMentor.
4
 
5
  import gradio as gr
6
  import json
7
- from .tools import generate_diagram as generate_diagram_core, ip_info, subnet_calculator
 
 
 
 
 
8
 
9
  def generate_diagram(ip_network: str, hosts_list: str, use_svg: bool = False):
10
  """
@@ -36,6 +41,31 @@ def generate_diagram(ip_network: str, hosts_list: str, use_svg: bool = False):
36
  return None, f"❌ Error: {str(e)}"
37
 
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  def create_interface():
40
  """Create the Gradio interface."""
41
 
@@ -81,7 +111,19 @@ def create_interface():
81
  title="Network Diagram Generator",
82
  description="Generate network diagrams (PNG by default, SVG optional)"
83
  )
84
-
 
 
 
 
 
 
 
 
 
 
 
 
85
  # Create main interface with custom header and description
86
  with gr.Blocks() as combined_app:
87
  # Header with logo
@@ -89,16 +131,13 @@ def create_interface():
89
 
90
  # Description
91
  gr.Markdown("""
92
- **IPMentor** is a comprehensive IPv4 networking toolkit that provides three powerful tools:
93
-
94
  - **IP Info**: Analyze IPv4 addresses with subnet masks, supporting decimal, binary, and CIDR formats
95
  - **Subnet Calculator**: Calculate subnets using different methods (max subnets, max hosts per subnet, and VLSM)
96
  - **Network Diagram**: Generate visual network diagrams with automatic subnet validation
97
-
98
- <a href="https://github.com/DavidLMS/ipmentor" target="_blank">Project on GitHub</a>
99
-
100
- <a href="https://huggingface.co/spaces/DavidLMS/ipmentor-demo" target="_blank">MCP Client chatbot demo</a>
101
-
102
  Choose a tab below to get started with your networking calculations and visualizations.
103
  """)
104
 
@@ -110,5 +149,7 @@ def create_interface():
110
  subnet_interface.render()
111
  with gr.Tab("Network Diagram"):
112
  diagram_interface.render()
 
 
113
 
114
  return combined_app
 
4
 
5
  import gradio as gr
6
  import json
7
+ from .tools import (
8
+ generate_diagram as generate_diagram_core,
9
+ ip_info,
10
+ subnet_calculator,
11
+ generate_subnetting_exercise as generate_exercise_core
12
+ )
13
 
14
  def generate_diagram(ip_network: str, hosts_list: str, use_svg: bool = False):
15
  """
 
41
  return None, f"❌ Error: {str(e)}"
42
 
43
 
44
+ def generate_exercise(num_subnets: str, use_vlsm: bool = False):
45
+ """
46
+ Generate a subnetting exercise.
47
+
48
+ Args:
49
+ num_subnets (str): Number of subnets to generate
50
+ use_vlsm (bool): Whether to use VLSM (different host counts per subnet)
51
+
52
+ Returns:
53
+ str: Exercise specification in JSON format
54
+ """
55
+ try:
56
+ num_subnets_int = int(num_subnets.strip())
57
+ if num_subnets_int < 1:
58
+ return json.dumps({"error": "Number of subnets must be at least 1"}, indent=2)
59
+
60
+ result_json = generate_exercise_core(num_subnets_int, use_vlsm)
61
+ return result_json
62
+
63
+ except ValueError:
64
+ return json.dumps({"error": "Invalid number of subnets"}, indent=2)
65
+ except Exception as e:
66
+ return json.dumps({"error": str(e)}, indent=2)
67
+
68
+
69
  def create_interface():
70
  """Create the Gradio interface."""
71
 
 
111
  title="Network Diagram Generator",
112
  description="Generate network diagrams (PNG by default, SVG optional)"
113
  )
114
+
115
+ exercise_interface = gr.Interface(
116
+ fn=generate_exercise,
117
+ api_name="generate_exercise",
118
+ inputs=[
119
+ gr.Textbox(label="Number of Subnets", placeholder="4", value="4"),
120
+ gr.Checkbox(label="Use VLSM (Variable Length Subnet Mask)", value=False)
121
+ ],
122
+ outputs=gr.Textbox(label="Exercise", lines=20, interactive=False),
123
+ title="Subnetting Exercise Generator",
124
+ description="Generate random subnetting exercises. Enable VLSM for variable host requirements per subnet, or disable for equal division."
125
+ )
126
+
127
  # Create main interface with custom header and description
128
  with gr.Blocks() as combined_app:
129
  # Header with logo
 
131
 
132
  # Description
133
  gr.Markdown("""
134
+ **IPMentor** is a comprehensive IPv4 networking toolkit that provides four powerful tools:
135
+
136
  - **IP Info**: Analyze IPv4 addresses with subnet masks, supporting decimal, binary, and CIDR formats
137
  - **Subnet Calculator**: Calculate subnets using different methods (max subnets, max hosts per subnet, and VLSM)
138
  - **Network Diagram**: Generate visual network diagrams with automatic subnet validation
139
+ - **Exercise Generator**: Generate random subnetting exercises for practice and learning
140
+
 
 
 
141
  Choose a tab below to get started with your networking calculations and visualizations.
142
  """)
143
 
 
149
  subnet_interface.render()
150
  with gr.Tab("Network Diagram"):
151
  diagram_interface.render()
152
+ with gr.Tab("Exercise Generator"):
153
+ exercise_interface.render()
154
 
155
  return combined_app