Skip to content

Commit

Permalink
adding check port functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
nadijagraca committed Sep 26, 2024
1 parent 61f34d8 commit c8d4401
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 12 additions & 0 deletions vizro-ai/examples/dashboard_ui/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import base64
import io
import logging
import socket

import pandas as pd

Expand Down Expand Up @@ -82,3 +83,14 @@ def format_output(generated_code):
generated_code += " app.run(port=8051)\n"

return generated_code


def check_available_port(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sk:
return sk.connect_ex(('127.0.0.1', port)) != 0


def find_available_port(base_port=8051):
while not check_available_port(base_port):
base_port += 1
return base_port
5 changes: 3 additions & 2 deletions vizro-ai/examples/dashboard_ui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import dash_bootstrap_components as dbc
import vizro.models as vm
from _utils import format_output
from _utils import format_output, find_available_port
from actions import data_upload_action, display_filename, save_files
from components import (
CodeClipboard,
Expand Down Expand Up @@ -264,10 +264,11 @@ def show_button(ai_response):
)
def run_generated_dashboard(n_clicks):
"""Runs vizro-ai generated dashboard in an iframe window."""
port = find_available_port()
if not n_clicks:
raise PreventUpdate
else:
subprocess.Popen(["python", "output_files/run_vizro_ai_output.py"])
subprocess.Popen(["python", "output_files/run_vizro_ai_output.py", str(port)])
iframe = html.Iframe(src="http://localhost:8051/", height="600px")
return True, iframe

Expand Down

0 comments on commit c8d4401

Please sign in to comment.