Ejemplo n.º 1
0
def page_layout():

    page_layouts = {'signin': signin_form(), 'default': user_profile_layout()}

    (routes, children) = zip(*page_layouts.items())

    @app.callback([Output('router', 'switch'),
                   Output('title', 'title')], [Input('loc', 'pathname')])
    def _router_callback(pathname):
        route = 'default'
        title = 'My Site: {}'.format(user_profile.user)

        log.info('_router_callback pathname=%s', pathname)

        if pathname:
            pathname = pathname[1:]
            if pathname in page_layouts:
                route = pathname

            if route == 'signin' and user_profile.user_signed_in():
                route = 'default'

        return route, title

    return html.Div([
        dhc.PageTitle(title='My Site', id='title'),
        dcc.Location(id='loc'),
        dhc.LayoutRouter(children, routes=routes, id='router')
    ])
def site_layout(app):

    # Process browser location change

    @app.callback([Output('title', 'title'),
                   Output('page-ref', 'children')], [Input('loc', 'pathname')])
    def _location_callback(pathname):
        title = 'My Site: {}'.format(pathname)
        return title, title

    return html.Div([
        dhc.PageTitle(title='My Site', id='title'),
        dhc.Location(id='loc'),
        html.H2(id='page-ref'),
        dcc.Link('page1', id='page1', href='/page1', refresh=False),
        dcc.Link('page2', id='page2', href='/page2', refresh=False)
    ])
Ejemplo n.º 3
0
def page_layout():

    # Define page content

    page_layouts = {
        'default': default_page(),
        'user': user_page(),
        'signin': signin_form(),
        'checkbox': checkbox_form(),
    }

    # Process browser location change. Select the page content based on the location

    @app.callback([Output('router', 'switch'),
                   Output('title', 'title')], [Input('loc', 'pathname')])
    def _router_callback(pathname):
        route = 'default'
        title = 'My Site: {}'.format(user_profile.user)

        log.info('pathname=%s', pathname)

        if pathname:
            pathname = pathname[1:]

            if pathname in page_layouts:
                route = pathname

        return route, title

    # Return static content common to all pages on the site

    (routes, children) = zip(*page_layouts.items())

    return html.Div([
        dhc.PageTitle(title='My Site', id='title'),
        dhc.Location(id='loc'),
        dhc.LayoutRouter(children, routes=routes, id='router')
    ])
Ejemplo n.º 4
0
    def pageLayout(self):
        """
        Called once on initialisation to provide the top-level page
        layout for the entire application.
        """

        def get_layout(route_ctx):
            args = arg_list(route_ctx.layout)
            if 'ctx' in args:
                route_ctx.login_manager = self.login_manager
                content = route_ctx.layout(route_ctx)
            else:
                content = route_ctx.layout()
            return content

        blueprint_routes = self.blueprint_routes

        # Iterate over all routes to register callbacks with dash

        for route, route_ctx in blueprint_routes.items():
            get_layout(route_ctx)

        # Define the dynamic top-level layout components and their
        # associated callback.

        page_content = html.Div(id='spa-page_content')
        page_title = dhc.PageTitle(title=self.title, id='spa-title')

        @self.callback(page_title.output.title, page_content.output.children,SpaComponents.url.input.href)
        def _display_page(href):
            title = SpaComponents.NOUPDATE
            page = SpaComponents.NOUPDATE

            url = urlsplit(href)

            pathname = re.sub(r"\/$", '', url.path)

            if pathname is None or pathname == '':
                pathname = '/'

            log.info('display_page href=%s', href)

            try:

                if pathname in blueprint_routes:
                    route_ctx = blueprint_routes[pathname]
                    route_ctx.url = url
                else:
                    raise Route404(f'Unknown route {pathname}')

                # If route has access guard in place call it

                if not route_ctx.access or route_ctx.access(route_ctx):
                    page = get_layout(route_ctx)
                    title = route_ctx.title

            except Exception as ex:
                msg = ex.message if hasattr(ex, 'message') else "???"
                log.info('Error pathname %s : %s', pathname, msg)
                page_content = self.show404(message=msg)

            return title, page

        # Render the navbar

        navbar = self.navBar(self.navitems) if self.navitems else None
        navbar_content = html.Div(navbar, id='spa-navbar_content')

        # @self.callback(navbar_content.output.children, SpaComponents.url.input.href)
        # def _display_navbar(href):
        #     log.info('display_navbar=%s', href)
        #     navbar = self.navBar(self.navitems) if self.navitems else None
        #     return navbar

        # Render the footer

        footer = self.footer()
        footer_content = html.Div(footer, id='spa-footer_content')

        # @self.callback(footer_content.output.children, SpaComponents.url.input.href)
        # def _display_footer(href):
        #     log.info('display_footer=%s', href)
        #     footer = self.footer()
        #     return footer

        # Block any further Dash callback registrations

        self._is_initialisation_completed = True

        # Return the top-level page layout

        layout = html.Div([
            SpaComponents.url,
            SpaComponents.redirect,
            page_title,
            navbar_content,
            html.Br(),
            html.Div([
                html.Div([
                    html.Div([], className="col-md-1"),
                    html.Div(page_content, id='page-content', className="col-md-10"),
                    html.Div([], className="col-md-1")
                ], className='row')
            ], className="container-fluid"),
            html.Div(id='null'),
            footer_content
        ])
        return layout
Ejemplo n.º 5
0
def ticker(ctx):

    log.info('/ticker')

    # http://localhost:8050/finance_explorer?tickers=TSLA

    querystring_name = 'tickers'

    stock_ticker_dropdown = dcc.Dropdown(
        id='stock_ticker',
        value=ctx.get_url_query_values(querystring_name),
        # name=querystring_name,
        options=[{
            'label': s[0],
            'value': str(s[1])
        } for s in zip(df.Stock.unique(), df.Stock.unique())],
        multi=True,
    )

    graphs = html.Div(id='graphs')

    # Callback to create the charts for requested tickers

    @demo.callback(graphs.output.children, [stock_ticker_dropdown.input.value])
    def _update_graph(tickers):
        global current_tickers
        log.info('_update_graph, stock_ticker_dropdown.input: %s', tickers)
        if tickers is None:
            tickers = current_tickers
        current_tickers = tickers
        return update_graph(tickers)

    # Callback to update the browser search-bar with the
    # selected tickers

    location = dhc.Location(id='redirect', refresh=False)
    title = dhc.PageTitle(title=ctx.title, id='ticker_title')

    @demo.callback([location.output.href, title.output.title],
                   [stock_ticker_dropdown.input.value])
    def _update_url(tickers):
        log.info('_update_url, stock_ticker_dropdown.input: %s', tickers)
        href = SpaComponents.NOUPDATE
        title = SpaComponents.NOUPDATE
        if tickers is not None:
            href = demo.url_for('ticker')
            urlargs = '+'.join(tickers)
            search = f'?{querystring_name}={urlargs}'
            href += search
            title = ctx.title = 'Ticker: ' + ','.join(tickers)
        return href, title

    # Layout the page

    return html.Div([
        location, title,
        html.H2('Finance Explorer'),
        html.Br(), stock_ticker_dropdown,
        html.Br(), graphs
    ],
                    className="container")