Skip to content

Commit

Permalink
refactor beginner-intermediate
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcSkovMadsen committed Jan 20, 2024
1 parent 2d57353 commit 2f8ceee
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ In this section you will learn the basics of developing efficiently in an editor
- display the app in a *simple* browser tab inside your editor if possible
- Inspect Panel objects via *hover* and `print`
- Inspect a components parameters via `.param` and `.param._repr_html_()`
- Debug with [Pdb](https://docs.python.org/3/library/pdb.html) by inserting a `breakpoint()`

:::{admonition} Note
Some of the features demonstrated in this guide might require special configuration of your editor. For configuration we refer you to the [Resources](#resources) section below and general resources on the web.
Expand Down Expand Up @@ -87,7 +86,7 @@ Open [http://localhost:5006/app](http://localhost:5006/app) in a browser.

It should look like

![Panel served app](../_static/images/develop_editor_panel_serve_before.png)
![Panel served app](../../_static/images/develop_editor_panel_serve_before.png)

Now change the

Expand All @@ -97,7 +96,7 @@ Now change the

It should look like

<video controls="" poster="../_static/images/develop_editor_panel_serve_after.png">
<video controls="" poster="../../_static/images/develop_editor_panel_serve_after.png">
<source src="https://assets.holoviz.org/panel/tutorials/develop_editor_serve_app.mp4" type="video/mp4" style="max-height: 400px; max-width: 100%;">
Your browser does not support the video tag.
</video>
Expand Down Expand Up @@ -145,21 +144,21 @@ Hover over the word `FastListTemplate`.

It would look something like

![Tooltip of FastListTemplate](../_static/images/develop_editor_hover.png)
![Tooltip of FastListTemplate](../../_static/images/develop_editor_hover.png)

:::{admonition} Info
:::{admonition} Note
The tooltip of Panel components normally provide an *example* code snippet and a *Reference* link. The *Reference* link makes it very easy to navigate to the reference guides on the Panel web site for more information.
:::

:::{admonition} Info
:::{admonition} Note
If your editor does not show any tooltips, then please refer to your editors documentation to figure out how to enable it.
:::

Hover again and click the *Reference* link <a href="https://panel.holoviz.org/reference/templates/FastListTemplate.html" target="_blank">https://panel.holoviz.org/reference/templates/FastListTemplate.html</a>.

This should open the `FastListTemplate` reference guide

[![FastListTemplate reference guide](../_static/images/develop_editor_reference_guide.png)](https://panel.holoviz.org/reference/templates/FastListTemplate.html)
[![FastListTemplate reference guide](../../_static/images/develop_editor_reference_guide.png)](https://panel.holoviz.org/reference/templates/FastListTemplate.html)

:::{admonition} Note
It is a great idea to use the *Example* code snippets and *Reference* links to speed up your workflow.
Expand Down Expand Up @@ -221,13 +220,13 @@ Replace `layout.servable()` with `layout[0].servable()` and save the file.

This will look like

![Layout[0]](../_static/images/develop_editor_layout0.png)
![Layout[0]](../../_static/images/develop_editor_layout0.png)

Replace `layout[0].servable()` with `layout[1].servable()` and save the file.

This will look like

![Layout[1]](../_static/images/develop_editor_layout1.png)
![Layout[1]](../../_static/images/develop_editor_layout1.png)

## Inspect component Parameters via `.param`

Expand All @@ -253,84 +252,7 @@ Open [http://localhost:5006](http://localhost:5006) in a browser.

It should look like

![.param and .param._repr_html_()](../_static/images/develop_editor_param.png)

## Debug your App with Pdb

A simple way to debug your apps that works in any editor is to insert a `breakpoint()`.

Copy the code below into a file named `app.py`.

```python
import panel as pn

pn.extension(design="material")

def handle_click(event):
breakpoint()

pn.widgets.Button(name="Click Me", on_click=handle_click, button_type="primary").servable()
```

Serve the app with `panel serve app.py --autoreload`.

Open [http://localhost:5006/app](http://localhost:5006/app) in a browser.

The app will look something like

![App with `Click Me` button](../_static/images/develop_editor_click_me.png)

Click the `Click Me` Button.

You terminal will look something like

```bash
$ panel serve app.py --autoreload
2024-01-20 08:12:09,512 Starting Bokeh server version 3.3.3 (running on Tornado 6.4)
2024-01-20 08:12:09,514 User authentication hooks NOT provided (default user enabled)
2024-01-20 08:12:09,516 Bokeh app running at: http://localhost:5006/app
2024-01-20 08:12:09,516 Starting Bokeh server with process id: 9768
2024-01-20 08:12:10,608 WebSocket connection opened
2024-01-20 08:12:10,608 ServerConnection created
--Return--
> /home/jovyan/app.py(6)handle_click()->None
-> breakpoint()
(Pdb)
```

Write `event` in the terminal. Press `ENTER`.

It should look like

![Breakpoint](../_static/images/develop_editor_breakpoint.png)

Write `help` and press `ENTER` for more info. It will look like

```bash
(Pdb) help

Documented commands (type help <topic>):
========================================
EOF c d h list q rv undisplay
a cl debug help ll quit s unt
alias clear disable ignore longlist r source until
args commands display interact n restart step up
b condition down j next return tbreak w
break cont enable jump p retval u whatis
bt continue exit l pp run unalias where

Miscellaneous help topics:
==========================
exec pdb
```
Write `c` and press `ENTER` to continue running the code and server.
:::{admonition} Note
For more about debugging with [Pdb](https://docs.python.org/3/library/pdb.html) and `breakpoint` please check out the [PDB Documentation](https://docs.python.org/3/library/pdb.html).
For *integrated debugging* in your editor, please refer to the [Resources](#resources) section below and general resources on the web.
:::
![.param and .param._repr_html_()](../../_static/images/develop_editor_param.png)

## Recap

Expand All @@ -340,13 +262,12 @@ You have learned to
- display the app in a *simple* browser tab inside your editor if possible
- Inspect Panel objects via *hover* and `print`
- Inspect a components parameters via `.param` and `.param._repr_html_()`
- Debug with [Pdb](https://docs.python.org/3/library/pdb.html) by inserting a `breakpoint()`

## Resources

### Tutorial
### Tutorials

- [Python Debugging with Pdb](https://realpython.com/python-debugging-pdb/)
- [Develop in an Editor (Intermediate)](../intermediate/develop_editor.md)

### How-to

Expand Down
159 changes: 159 additions & 0 deletions doc/tutorials/beginner/panel_serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# Serve Panel Apps

In this section you will learn the basics of serving Panel apps:

- serve your app with a command like `panel serve app.py` or `panel serve app2.ipynb`.
- serve with *auto reload* by adding the flag `--autoreload`.
- stop your server with `CTRL+C`.

## Serve an app from a `.py` file

The simplest Panel `.py` file could look like this:

```python
import panel as pn

pn.extension()

pn.panel("Hello World").servable()
```

Copy the code above into a file named `app.py`.

Save the file.

:::{admonition} Note
The lines in the app.py file refer to

- `panel`: The Panel python package. Its a convention to import it as `pn`.
- `pn.extension()`: Loads javascript dependencies and configures Panel.
- `pn.panel(...)`: Creates a *displayable* Panel component.
- `.servable()`: Displays the component in a *server app*.
:::

Run the live server with

```python
panel serve app.py --autoreload
```

It will look like

```bash
$ panel serve app.py --autoreload
2024-01-17 15:49:11,443 Starting Bokeh server version 3.3.2 (running on Tornado 6.3.3)
2024-01-17 15:49:11,444 User authentication hooks NOT provided (default user enabled)
2024-01-17 15:49:11,450 Bokeh app running at: http://localhost:5006/app
2024-01-17 15:49:11,450 Starting Bokeh server with process id: 47256
```

:::{admonition} Note
The command `panel serve app.py --autoreload` refers to:

- `panel`: the panel executable.
- `serve`: the command you want panel to run
- `app.py`: the file `app.py` you want to serve
- `--autoreload`: make the server restart after code changes. Use this for development only.
:::

In the output, there's a line with something like:

```bash
Bokeh app running at: http://localhost:5006/app
```

That line shows the URL where your app is being served, in your local machine.

You will also notice that `Bokeh server` is mentioned. Its because the Panel server is an extension of the Bokeh server.

Open your browser at [http://localhost:5006/app](http://localhost:5006/app).

The application will look like.

![Panel serve single .py file](../../_static/images/panel-serve-py-app.png).

Now stop the server by pressing `CTRL+C` one or more times in the terminal.

## Serve an app from a notebook file

The simplest Panel notebook file could look like:

![Panel Notebook App](../../_static/images/panel-serve-ipynb-notebook.png).

Copy the 2 code cells below into a clean notebook named `app2.ipynb`.

```python
import panel as pn

pn.extension()
```

```python
pn.panel("Hello Notebook World").servable()
```

:::{admonition} Note
The lines in the `app2.ipynb` file refer to

- `panel`: The Panel python package. Its a convention to import it as `pn`.
- `pn.extension()`: **Loads the [`pyviz_comms`](https://github.com/holoviz/pyviz_comms) notebook extension**, loads javascript dependencies and configures Panel.
- `pn.panel(...)`: Creates a *displayable* Panel component. **The component can be displayed directly in the notebook**.
- `.servable()`: Displays the component in a *server app*.
:::

Run the cells in the notebook and save it if you have not already done it.

Run the Panel server with

```bash
panel serve app2.ipynb --autoreload
```

It will look like

```bash
$ panel serve app2.ipynb --autoreload
2024-01-17 21:05:32,338 Starting Bokeh server version 3.3.3 (running on Tornado 6.4)
2024-01-17 21:05:32,339 User authentication hooks NOT provided (default user enabled)
2024-01-17 21:05:32,342 Bokeh app running at: http://localhost:5006/app2
2024-01-17 21:05:32,342 Starting Bokeh server with process id: 42008
```

:::{admonition} Note
The command `panel serve app2.ipynb --autoreload` refers to:

- `panel`: the panel executable.
- `serve`: the command you want panel to run
- `app.py`: the file `app2.ipynb` you want to serve
- `--autoreload`: make the server restart after code changes. Use this for development only.
:::

In the output, there's a line with something like:

```bash
Bokeh app running at: http://localhost:5006/app2
```

That line shows the URL where your app is being served, in your local machine.

Open your browser at [http://localhost:5006/app2](http://localhost:5006/app2).

The application will look like.

![Panel serve single .ipynb file](../../_static/images/panel-serve-ipynb-app.png).

Now stop the server by pressing `CTRL+C` one or more times in the terminal.

## Recap

You can

- serve your app with a command like `panel serve app.py` or `panel serve app2.ipynb`.
- serve with *auto reload* by adding the flag `--autoreload`.
- stop your server with `CTRL+C`.

## Resources

### Tutorials

- [Serve Panel Apps (Intermediate)](../intermediate/panel_serve.md)
8 changes: 4 additions & 4 deletions doc/tutorials/develop_notebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ It should look like

![Panel Notebook App](../_static/images/develop_notebook_simple_example.png)

:::{admonition} Info
:::{admonition} Note
The code in the notebook refer to

- `panel`: The Panel python package. Its a convention to import it as `pn`.
Expand All @@ -74,7 +74,7 @@ The code in the notebook refer to
- `.servable()`: Displays the component in a *server app*.
:::

:::{admonition} Info
:::{admonition} Note
The little, blue Panel icon above the notebook will launch a *preview* of your app. We call this icon the *Jupyter Panel Preview* icon.
:::

Expand Down Expand Up @@ -104,7 +104,7 @@ The *preview* will reload and look like

![Reloaded Preview](../_static/images/develop_notebook_simple_example_add_hello_again.png)

:::{admonition} Info
:::{admonition} Note
To enable a more efficient workflow you can check the *Render on Save* checkbox. This will *auto reload* your app when the notebook is saved.
:::

Expand All @@ -131,7 +131,7 @@ Watch the video below to learn how the techniques above can be used to develop a

## Serve your app with autoreload

:::{admonition} Info
:::{admonition} Note
A currently faster alternative to the *Jupyter Panel Preview* is serving the notebook externally with autoreload using a command like `panel serve app.ipynb --autoreload`.
:::

Expand Down
Loading

0 comments on commit 2f8ceee

Please sign in to comment.