GeezerVison is showing github.com — Docs + Launch hotfixes (#1272) · datapane/datapane@5ccad56
File treeExpand file treeCollapse file tree Open diff view settings Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: public-docs/Taskfile.yml +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,6 +23,13 @@ tasks: | 23 | 23 | - cmd: poetry run ./nbbuild.sh | 24 | 24 | - cmd: poetry run mkdocs build | 25 | 25 |
| | 26 | + build-docs-only: | | 27 | + desc: "Locally build the docs only (don't run notebooks)" | | 28 | + env: | | 29 | + DATAPANE_CDN_BASE: '{{ .DATAPANE_CDN_BASE | default "" }}' | | 30 | + cmds: | | 31 | + - cmd: poetry run mkdocs build | | 32 | + | 26 | 33 | build:inline-statics: | 27 | 34 | desc: "Build a package ready for a deploy, with report statics loaded into the build" | 28 | 35 | cmds: | |
Collapse file Copy file name to clipboardExpand all lines: public-docs/docs/apps/advanced.md +30-26Lines changed: 30 additions & 26 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -15,21 +15,21 @@ import datapane as dp | 15 | 15 | # a globally-accessible DataFrame | 16 | 16 | df = data.iris() | 17 | 17 |
| 18 | | -def filter_df(params): | 19 | | - return dp.DataTable(df[params['column']]) | | 18 | +def filter_df(column: str): | | 19 | + return dp.DataTable(df[column]) | 20 | 20 |
| 21 | | -app = dp.Blocks( | | 21 | +blocks = dp.Blocks( | 22 | 22 | dp.Form( | 23 | 23 | on_submit=filter_df, | 24 | | - controls=[dp.TextBox(name='name')] | | 24 | + controls=dp.Controls(column = dp.Choice(options=list(df.columns), label="Select column to filter")) | 25 | 25 | ) | 26 | 26 | ) | 27 | | -dp.serve_app(app) | | 27 | +dp.serve_app(blocks) | 28 | 28 | ``` | 29 | 29 |
| 30 | 30 | In the example above, `df` is referenced by `filter_df` and can be accessed in that function by all users when the function is called. It's important to note that Datapane supports multiple users by default, and Datapane will call ``filter_df` automatically for each user individually upon submitting the form. | 31 | 31 |
| 32 | | -... warning: | | 32 | +!!! warning: | 33 | 33 | Datapane apps support multiple users via threads, so normal considerations apply when modifying shared data structures across different threads | 34 | 34 |
| 35 | 35 | When shutting down the server, no state is preserved, this means that all users will lose any unsaved data and will need to reload their apps within the browser upon restarting the server again. | @@ -46,34 +46,35 @@ We have mentioned that a backend function receipts parameters as input and retur | 46 | 46 | These are presented to the User in their App for performing further input that is in return sent to this __nested__ function - we term this _function chaining_. The functions defined and returned in these nested _Compute Blocks_ can be `lambdas`, previously defined / inline functions, and can, most importantly, refer to and capture existing variables in scope. This means it's possible to collect multiple pieces of input from a user, return per-user data, and even dynamically generate future input based on existing input - Python's dynamic nature makes quite advanced scenarios possible. This may be demonstrated best with an example, | 47 | 47 |
| 48 | 48 | ```python | | 49 | +from pathlib import Path | | 50 | +import pandas as pd | 49 | 51 | import datapane as dp | 50 | 52 |
| 51 | | -def load_file(file): | | 53 | +def load_file(file: Path): | 52 | 54 | # load the user's input into the file | 53 | | - df = pd.load(file) | | 55 | + df = pd.read_csv(file) | 54 | 56 |
| 55 | | - def process_df(col_name: str) | | 57 | + def process_df(col_name: str): | 56 | 58 | # a nested function which has access to `df` loaded in the previous interaction | 57 | | - return df[col_name] | | 59 | + return dp.DataTable(df[col_name]) | 58 | 60 |
| 59 | 61 | return [ | 60 | 62 | df.describe(), | 61 | 63 | # return a new Form that calls the nested function | 62 | | - dp.Form(on_submit=process_df, controls=[col_name=dp.TextBoc()]) | | 64 | + dp.Form(on_submit=process_df, controls=dict(col_name=dp.TextBox())) | 63 | 65 | ] | 64 | 66 |
| 65 | | -app = dp.Blocks( | | 67 | +blocks = dp.Blocks( | 66 | 68 | dp.Form( | 67 | 69 | on_submit=load_file, | 68 | | - controls=[file=dp.File()] | | 70 | + controls=dict(file=dp.File()) | 69 | 71 | ) | 70 | 72 | ) | 71 | | -dp.serve_app(app) | | 73 | +dp.serve_app(blocks) | 72 | 74 | ``` | 73 | 75 |
| 74 | 76 | In the above, the function chaining is isolated for each user, and uploaded `df` is stored independently to other users currently using the app. | 75 | 77 |
| 76 | | - | 77 | 78 | ## Function Caching | 78 | 79 |
| 79 | 80 | Your backend function may perform complex calculations and thus take a long time to finish and/or is memory/compute intensive - this can slow down he performance of your app and frustrate users. To help, Datapane provides the ability to cache backend functions and reuse their results when called via a _Compute Block_. | @@ -90,19 +91,21 @@ import time | 90 | 91 | import datapane as dp | 91 | 92 |
| 92 | 93 | # this function is cached when ever the same x and y values are seen | 93 | | -def calc(params): | | 94 | +def calc(x: int, y: int): | 94 | 95 | time.sleep(1) | 95 | | - return params['x'] * params['y'] | | 96 | + z = x+y | | 97 | + return dp.BigNumber("Result", x+y) | 96 | 98 |
| 97 | | -app = dp.Blocks( | | 99 | +blocks = dp.Blocks( | 98 | 100 | dp.Form( | 99 | 101 | on_submit=calc, | 100 | | - controls=[dp.NumberBox('x'), dp.NumberBox('y', initial=0)], | 101 | | - cache=True | | 102 | + controls=[dp.NumberBox('x', initial=2), dp.NumberBox('y', initial=3)], | | 103 | + cache=True, | | 104 | + label="Add values" | 102 | 105 | ) | 103 | 106 | ) | 104 | 107 |
| 105 | | -dp.serve_app(app) | | 108 | +dp.serve_app(blocks) | 106 | 109 | ``` | 107 | 110 |
| 108 | 111 | ## Session State | @@ -121,20 +124,21 @@ In the example below, we use the `session` variable to accumulate and modify a v | 121 | 124 | ```python | 122 | 125 | import datapane as dp | 123 | 126 |
| 124 | | -def calc(params, session: dict): | | 127 | +def calc(params: dict, session: dict): | 125 | 128 | # read a value from the per user-session dict | 126 | 129 | acc = session.get('acc', 0) | 127 | 130 | result = params['x'] * params['y'] + acc | 128 | 131 | # store back in the session | 129 | 132 | session['acc'] = result | 130 | | - return dp.Text(result) | | 133 | + return dp.BigNumber("Result", result) | 131 | 134 |
| 132 | | -app = dp.Blocks( | | 135 | +blocks = dp.Blocks( | 133 | 136 | dp.Form( | 134 | 137 | on_submit=calc, | 135 | | - controls=[dp.NumberBox('x'), dp.NumberBox('y')], | | 138 | + controls=[dp.NumberBox('x', initial=2), dp.NumberBox('y', initial=3)], | | 139 | + label="Multiply and accumulate values to state", | 136 | 140 | ) | 137 | 141 | ) | 138 | 142 |
| 139 | | -dp.serve_app(app) | | 143 | +dp.serve_app(blocks) | 140 | 144 | ``` |
Collapse file Copy file name to clipboardExpand all lines: public-docs/docs/apps/blocks.md +10-12Lines changed: 10 additions & 12 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | Compute blocks, such as `dp.Form`, allow you to take parameters from end-users or update your app dynamically by triggering backend functions. When you add a Compute block, you specify a Python function which returns... you guessed it, blocks! The use of compute blocks require a running server, so won't they work in reports. | 2 | 2 |
| 3 | | - | 4 | 3 | ## Forms | 5 | 4 |
| 6 | 5 | Build forms using [dp.Form][datapane.blocks.compute.Form], which expose controls, such as textboxes, file uploads, and number ranges. When a user submits the form, it runs the backend Python function you have chosen, and automatically updates the user's display with the result. | @@ -10,17 +9,17 @@ This means you can calculate complex analytics on demand in response to user inp | 10 | 9 | ```python | 11 | 10 | import datapane as dp | 12 | 11 |
| 13 | | -def f(params): | 14 | | - first_name = params["first_name"] | | 12 | +def f(first_name: str): | 15 | 13 | return dp.Text(f"Hello, {first_name}!") | 16 | 14 |
| 17 | | -app = dp.Blocks( | | 15 | +blocks = dp.Blocks( | 18 | 16 | dp.Text("Welcome to my app"), | 19 | 17 | dp.Form(on_submit=f, | 20 | | - controls=dp.Controls(first_name=dp.TextBox())), | | 18 | + controls=dp.Controls(first_name=dp.TextBox()), | | 19 | + label="Enter your name:"), | 21 | 20 | ) | 22 | 21 |
| 23 | | -dp.serve_app(app) | | 22 | +dp.serve_app(blocks) | 24 | 23 | ``` | 25 | 24 |
| 26 | 25 | You typically provide `dp.Form` with two parameters: | @@ -42,16 +41,15 @@ In the example below, the `on_load` parameter to `dp.Dynamic` - this instructs t | 42 | 41 | from datetime import datetime | 43 | 42 | import datapane as dp | 44 | 43 |
| 45 | | -def get_time(): | 46 | | - return dp.Text(datetime.now().time().isoformat()) | | 44 | +def get_time() -> str: | | 45 | + return datetime.now().time().isoformat() | 47 | 46 |
| 48 | | -app = dp.Blocks( | 49 | | - dp.Text(f"This app was created at {get_time()}"), | 50 | | - dp.Text("The current time is ..."), | | 47 | +blocks = dp.Blocks( | | 48 | + dp.Text(f"This app was created at {get_time()},\n the app was loaded at..."), | 51 | 49 | dp.Dynamic(on_load=get_time) | 52 | 50 | ) | 53 | 51 |
| 54 | | -dp.serve_app(app) | | 52 | +dp.serve_app(blocks) | 55 | 53 | ``` | 56 | 54 |
| 57 | 55 | `dp.Dynamic` also has an `on_timer` parameter which can be used to call a backend function on a regular schedule - see the [API docs][datapane.blocks.compute.Dynamic] for more information. | |
Collapse file Copy file name to clipboardExpand all lines: public-docs/docs/apps/deployment.md +30-1Lines changed: 30 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +You can host a Datapane app from inside your current Python environment, whether that's your local machine, or a hosted notebook like Colab or Databricks. | | 2 | + | | 3 | +## Local App Serving | | 4 | + | | 5 | +To host your app, run `dp.serve_app`. | | 6 | + | | 7 | +!!! note | | 8 | + A port will be chosen automatically, but you can manually specify `port` and `host`. | | 9 | + | | 10 | +```python | | 11 | +import datapane as dp | | 12 | + | | 13 | +app = dp.Blocks(...) | | 14 | + | | 15 | +dp.serve_app(app) | | 16 | +``` | | 17 | + | | 18 | +### Creating a public URL | | 19 | + | | 20 | +Datapane has built-in support for ngrok, which allows you to create a public, shareable URL for your data app. Simply add `public=True` to your serve command. This will prompt you to enter your ngrok token, and will return a URL which you can share. | | 21 | + | | 22 | +```python | | 23 | +dp.serve_app(app, public=True) | | 24 | +``` | | 25 | + | | 26 | +### Embed mode | | 27 | + | | 28 | +If you are embedding your app in a third-party product, you will need to pass `embed_mode=True` to `dp.serve`. This disables specific security policies which will not work within an `iframe`. | | 29 | + | 1 | 30 | ## Datapane Cloud | 2 | 31 |
| 3 | | -Datapane Cloud will shortly provide App hosting specific and optimised to the needs of App running and hosting, including access-controls, security and authentication features that organisations require. | | 32 | +Datapane Cloud will shortly provide App hosting specific and optimized to the needs of App running and hosting, including access-controls, security and authentication features that organizations require. | 4 | 33 |
| 5 | 34 | At the same time, you can easily host your app via any web server hosting service (especially those that work with Python / WSGI-based apps), including the services listed here. | 6 | 35 |
| |
Collapse file Copy file name to clipboardExpand all lines: public-docs/docs/apps/example.md +15-22Lines changed: 15 additions & 22 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -# An example app | 2 | | - | 3 | | -This example demonstrates an app that allows the user to visualise different dimensions of the Iris flower dataset. | 4 | | - | | 1 | +This example demonstrates an app that allows the user to visualize different dimensions of the Iris flower dataset. | 5 | 2 |
| 6 | 3 | ```python | | 4 | +import random | 7 | 5 | import altair as alt | 8 | 6 | import datapane as dp | 9 | 7 | from vega_datasets import data | 10 | 8 |
| | 9 | +# load the dataset as a global value | 11 | 10 | dataset = data.iris() | 12 | 11 | columns = list(dataset.columns) | 13 | 12 |
| 14 | | - | 15 | | -def plot(params): | 16 | | - global dataset | 17 | | - | | 13 | +def plot(x_axis: str, y_axis: str, color: str) -> dp.Plot: | | 14 | + """Create and return the plot""" | 18 | 15 | fig = ( | 19 | 16 | alt.Chart(dataset) | 20 | 17 | .mark_point() | 21 | 18 | .encode( | 22 | | - x=alt.X(params["x_axis"], scale=alt.Scale(zero=False)), | 23 | | - y=alt.X(params["y_axis"], scale=alt.Scale(zero=False)), | 24 | | - color=params["color"], | | 19 | + x=alt.X(x_axis, scale=alt.Scale(zero=False)), | | 20 | + y=alt.X(y_axis, scale=alt.Scale(zero=False)), | | 21 | + color=color, | 25 | 22 | tooltip=columns, | 26 | 23 | ) | 27 | 24 | ) | 28 | 25 |
| 29 | | - return dp.Plot(fig, name="plot") | 30 | | - | 31 | | - | 32 | | -plot({"x_axis": "sepalLength", "y_axis": "sepalWidth", "color": "species"}) | | 26 | + return dp.Plot(fig) | 33 | 27 |
| | 28 | +# App setup | 34 | 29 | controls = dp.Controls( | 35 | | - dp.Choice("x_axis", options=columns), | 36 | | - dp.Choice("y_axis", options=columns), | 37 | | - dp.Choice("color", options=columns), | | 30 | + dp.Choice("x_axis", options=columns, initial=random.choice(columns)), | | 31 | + dp.Choice("y_axis", options=columns, initial=random.choice(columns)), | | 32 | + dp.Choice("color", options=columns, initial=random.choice(columns)), | 38 | 33 | ) | 39 | | - | 40 | | -v = dp.Blocks( | | 34 | +blocks = dp.Blocks( | 41 | 35 | "# Iris Dataset Plotter", | 42 | 36 | dp.Form(plot, submit_label="Plot", controls=controls), | 43 | 37 | ) | 44 | | - | 45 | | -dp.serve_app(v) | | 38 | +dp.serve_app(blocks) | 46 | 39 | ``` | 47 | 40 |
| 48 | 41 | <!-- We've deployed the app to Fly.io using our [deployment docs](/deployment/deployed_apps/). You can view it here: | |
Collapse file Copy file name to clipboardExpand all lines: public-docs/docs/apps/functions-controls.md +3-4Lines changed: 3 additions & 4 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -42,12 +42,11 @@ dp.Form(on_submit=f, | 42 | 42 |
| 43 | 43 | ### Returning Values | 44 | 44 |
| 45 | | -Datapane functions can return any [Datapane block](../display_blocks.ipynb) or [raw Python objects](../display_blocks.ipynb#automatic-conversion) that are supported by Datapane. You can return these directly, as a list, or return a layout or container block like `dp.Blocks` or `dp.Group` for a more advanced layout. | | 45 | +Datapane functions can return any [Datapane block](../blocks/display-blocks.ipynb) or [raw Python objects](../blocks/display-blocks.ipynb#automatic-conversion) that are supported by Datapane. You can return these directly, as a list, or return a layout or container block like `dp.Blocks` or `dp.Group` for a more advanced layout. | 46 | 46 |
| 47 | 47 | !!! tip | 48 | 48 | You can even return compute blocks like `dp.Form` and `dp.Dynamic` from a function, allowing you to chain functions together that build more complex operations and interactive apps, such as wizards. | 49 | 49 |
| 50 | | - | 51 | 50 | ## Controls | 52 | 51 |
| 53 | 52 | Controls are used to define the specific parameters to present to the user for input, such as a `TextField`, a number `Range`, or even a `File` upload. | @@ -56,7 +55,7 @@ These parameters are collected from the user and passed by Datapane into the res | 56 | 55 |
| 57 | 56 | When the user submits the form, Datapane automatically handles uploading the user's input, validates it, converts it to the correct type, and provides initial values if none are given. From there, Datapane calls the Compute function with the params in the `params` dict as mentioned [above](#compute-functions). | 58 | 57 |
| 59 | | -When adding a Compute Block to your code, you pass in the collection of parameters that make up your contols via a `dp.Controls` object. | | 58 | +When adding a Compute Block to your code, you pass in the collection of parameters that make up your controls via a `dp.Controls` object. | 60 | 59 |
| 61 | 60 | ```python | 62 | 61 | dp.Form(on_submit=f, | @@ -89,4 +88,4 @@ The built-in parameters include, | 89 | 88 | | [DateTime][datapane.blocks.parameters.DateTime] | `datetime.datetime` | Select a date and a time | | 90 | 89 | | [File][datapane.blocks.parameters.File] | `pathlib.Path`| Upload a file | | 91 | 90 |
| 92 | | -The [Controls API docs](../../reference/apps/controls.ipynb) also include detailed information and live samples of each parameter type. | | 91 | +The [Controls API docs](../reference/apps/controls.ipynb) also include detailed information and live samples of each parameter type. |
Collapse file Copy file name to clipboardExpand all lines: public-docs/docs/apps/overview.md +9-9Lines changed: 9 additions & 9 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -12,22 +12,23 @@ In just a few lines we have built a simple data app that allows multiple users t | 12 | 12 | from vega_datasets import data | 13 | 13 | import datapane as dp | 14 | 14 |
| | 15 | +# a globally-accessible DataFrame | 15 | 16 | df = data.iris() | 16 | 17 |
| 17 | | -def filter_df(params): | | 18 | +def filter_df(column: str): | 18 | 19 | # Our dynamic function | 19 | | - return dp.DataTable(df[params['column']]) | | 20 | + return dp.DataTable(df[column]) | 20 | 21 |
| 21 | 22 | # We define the App similar to a Report | 22 | | -app = dp.Blocks( | | 23 | +blocks = dp.Blocks( | 23 | 24 | dp.Form( | 24 | 25 | on_submit=filter_df, | 25 | | - controls=[dp.TextBox(name='name')] | | 26 | + controls=dp.Controls(column=dp.Choice(options=list(df.columns), label="Select column to filter")) | 26 | 27 | ) | 27 | 28 | ) | 28 | 29 |
| 29 | 30 | # Start serving the app (by default on http://localhost:8000) | 30 | | -dp.serve_app(app) | | 31 | +dp.serve_app(blocks) | 31 | 32 | ``` | 32 | 33 |
| 33 | 34 | <!-- TODO - embed this app... --> | @@ -36,10 +37,9 @@ dp.serve_app(app) | 36 | 37 |
| 37 | 38 | Apps build upon Reports and add a few simple concepts to make them dynamic: | 38 | 39 |
| 39 | | -- [Compute Blocks](./blocks.md), such as [dp.Form][datapane.blocks.compute.Form] and [dp.Dynamic][datapane.blocks.compute.Dynamic] are added to your app alongside any static [Display](../display_blocks.ipynb) and [Layout](../layout_blocks.ipynb) blocks, and provide the interface into backend functions. | 40 | | -- [Parameters](./functions_controls.md), such as [dp.TextBox][datapane.blocks.parameters.TextBox] above, and more complex controls such as [dp.Range][datapane.blocks.parameters.Range], provide an interactive set of Controls to use in your Forms in order to allow your viewers to specify parameters. | 41 | | -- [Backend functions](./functions_controls.md), such as `filter_df` above, take these parameters, run any processing needed, and return Display and Layout blocks. | 42 | | - | | 40 | +- [Compute Blocks](./blocks.md), such as [dp.Form][datapane.blocks.compute.Form] and [dp.Dynamic][datapane.blocks.compute.Dynamic] are added to your app alongside any static [Display](../blocks/display-blocks.ipynb) and [Layout](../blocks/layout-blocks.ipynb) blocks, and provide the interface into backend functions. | | 41 | +- [Parameters](./functions-controls.md), such as [dp.TextBox][datapane.blocks.parameters.TextBox] above, and more complex controls such as [dp.Range][datapane.blocks.parameters.Range], provide an interactive set of Controls to use in your Forms in order to allow your viewers to specify parameters. | | 42 | +- [Backend functions](./functions-controls.md), such as `filter_df` above, take these parameters, run any processing needed, and return Display and Layout blocks. | 43 | 43 |
| 44 | 44 | Most other data app frameworks work by running your app from top to bottom every time something changes. | 45 | 45 |
| |
|