Skip to content

Commit

Permalink
Switch from React/JSX to Python for constructing nav() markup
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert committed Apr 25, 2022
1 parent 44d850f commit 5310a0b
Show file tree
Hide file tree
Showing 9 changed files with 462 additions and 3,024 deletions.
37 changes: 30 additions & 7 deletions shiny/_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import contextlib
import functools
import importlib
import inspect
import os
import random
import secrets
import sys
import tempfile

from typing import (
Callable,
Awaitable,
Expand All @@ -8,13 +18,6 @@
Any,
cast,
)
import functools
import os
import sys
import tempfile
import importlib
import inspect
import secrets

if sys.version_info >= (3, 10):
from typing import TypeGuard
Expand All @@ -33,6 +36,26 @@ def rand_hex(bytes: int) -> str:
return format_str.format(secrets.randbits(bytes * 8))


def private_random_int(min: int, max: int) -> str:
with private_seed():
return str(random.randint(min, max))


@contextlib.contextmanager
def private_seed():
state = random.getstate()
global own_random_state
try:
if own_random_state is not None:
random.setstate(own_random_state)
yield
finally:
own_random_state = random.getstate()
random.setstate(state)


own_random_state = None

# ==============================================================================
# Async-related functions
# ==============================================================================
Expand Down
15 changes: 11 additions & 4 deletions shiny/examples/nav/app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from typing import List
from typing import List, Union

from shiny import *
from htmltools import JSXTag, h4
from shiny.ui import h4
from shiny.ui._navs import Nav, NavMenu # TODO: export this via shiny.types
from fontawesome import icon_svg as icon


def nav_items(prefix: str) -> List[JSXTag]:
def nav_items(prefix: str) -> List[Union[Nav, NavMenu]]:
return [
ui.nav("a", prefix + ": tab a content"),
ui.nav("b", prefix + ": tab b content"),
Expand All @@ -21,6 +22,9 @@ def nav_items(prefix: str) -> List[JSXTag]:
ui.nav_menu(
"Other links",
ui.nav("c", prefix + ": tab c content"),
"----",
"Plain text",
"----",
ui.nav_item(
ui.a(
icon("r-project"),
Expand All @@ -39,6 +43,7 @@ def nav_items(prefix: str) -> List[JSXTag]:
title="page_navbar()",
bg="#0062cc",
inverse=True,
id="navbar_id",
footer=ui.div(
{"style": "width:80%;margin: 0 auto"},
ui.h4("navs_tab()"),
Expand All @@ -56,7 +61,9 @@ def nav_items(prefix: str) -> List[JSXTag]:


def server(input: Inputs, output: Outputs, session: Session):
pass
@reactive.Effect()
def _():
print("Current navbar page: ", input.navbar_id())


app = App(app_ui, server)
2 changes: 1 addition & 1 deletion shiny/ui/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

from .._docstring import add_example
from ._html_dependencies import jqui_deps
from ._page import get_window_title
from ._utils import get_window_title


# TODO: make a python version of the layout guide?
Expand Down
14 changes: 1 addition & 13 deletions shiny/ui/_html_dependencies.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Union
from typing import List

from htmltools import HTML, HTMLDependency

Expand Down Expand Up @@ -29,18 +29,6 @@ def bs3compat_deps() -> HTMLDependency:
)


def nav_deps(
include_bootstrap: bool = True,
) -> Union[HTMLDependency, List[HTMLDependency]]:
dep = HTMLDependency(
name="bslib-navs",
version="1.0",
source={"package": "shiny", "subdir": "www/shared/bslib/dist/"},
script={"src": "navs.min.js"},
)
return [dep, *bootstrap_deps()] if include_bootstrap else dep


def ionrangeslider_deps() -> List[HTMLDependency]:
return [
HTMLDependency(
Expand Down
Loading

0 comments on commit 5310a0b

Please sign in to comment.