Skip to content
Snippets Groups Projects
Commit 90ea94ba authored by David Rouquet's avatar David Rouquet
Browse files

initialize app structure

parent affa7cb2
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:1d7eebc2-9325-42ae-8269-47b3091e052c tags:
``` python
from src.data import Data
from src.view import View
from src.appli import Appli
data = Data()
layout = View(data).layout
appli = Appli(layout, data)
```
from jupyter_dash import JupyterDash as Dash
import dash_bootstrap_components as dbc
from src.callbacks import register_callbacks
class Appli:
def __init__(self, layout, data):
server_url, host, port, proxy, base_path = self._get_tl_config()
app = Dash(
server_url=server_url,
requests_pathname_prefix=base_path,
)
app.layout = layout
app.data = data
register_callbacks(app)
app.run_server(mode="inline", host=host, port=port, proxy=proxy)
def _get_tl_config(self):
import socket, errno, os
# Find a free port
host = "0.0.0.0"
port = 8050
end = 9999
found = False
while not found:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind((host, port))
found = True
except socket.error as e:
if e.errno == errno.EADDRINUSE:
port = port + 1
if (port > end):
raise "No available APP port"
else:
raise e
if (os.getenv("HOST", None) is not None):
proto = os.getenv("PROTO")
actualhost = os.getenv("JUPYTER_HOST", os.getenv("VOILA_HOST", ""))
localport = os.getenv("PORT", 80)
intermediatehost = os.getenv("HOST", "localhost")
base_path = f"/{actualhost}/app_proxy/{port}/"
proxified= f"{proto}://{intermediatehost}:{localport}{base_path}"
localurl = f"http://{host}:{port}"
proxy = f"{localurl}::{proxified}"
return ((proxified, host, port, proxy, base_path))
return ((f"http://localhost:{port}", host, port, None, "/"))
from dash import Output, Input
def register_callbacks(app):
@app.callback(
Output(component_id="my-output", component_property="children"),
Input(component_id="my-input", component_property="value")
)
def update_output_div(input_value):
return f"Output: {input_value}"
\ No newline at end of file
class Component:
def __init__(self, data):
self.data = data
def build(self):
raise Error("Not implemented")
from src.components.component import Component
from dash import html, dcc
class Window(Component):
def build(self):
return self._layout()
def _layout(self):
return html.Div(children=
[
html.H6("Change the value in the text box to see callbacks in action!"),
html.Div(children=
[
"Input: ",
dcc.Input(
id="my-input",
value=self.data.initial_value,
type="text"
)
]),
html.Br(),
html.Div(id="my-output")
]
)
class Data:
def __init__(self):
self.initial_value = "my initial value"
\ No newline at end of file
from src.components.window import Window
class View:
def __init__(self, data):
self.layout = Window(data).build()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment