1 7
import os
2 7
import time
3

4 7
import param
5 7
import pytest
6 7
import requests
7

8 7
from panel.config import config
9 7
from panel.io import state
10 7
from panel.models import HTML as BkHTML
11 7
from panel.pane import Markdown
12 7
from panel.io.server import serve, set_curdoc
13 7
from panel.widgets import Button
14

15

16 7
def test_get_server(html_server_session):
17 7
    html, server, session = html_server_session
18

19 7
    assert server.port == 5006
20 7
    root = session.document.roots[0]
21 7
    assert isinstance(root, BkHTML)
22 7
    assert root.text == '<h1>Title</h1>'
23

24

25 7
def test_server_update(html_server_session):
26 7
    html, server, session = html_server_session
27

28 7
    html.object = '<h1>New Title</h1>'
29 7
    session.pull()
30 7
    root = session.document.roots[0]
31 7
    assert isinstance(root, BkHTML)
32 7
    assert root.text == '&lt;h1&gt;New Title&lt;/h1&gt;'
33

34

35 7
def test_server_change_io_state(html_server_session):
36 7
    html, server, session = html_server_session
37

38 7
    def handle_event(event):
39 7
        assert state.curdoc is session.document
40

41 7
    html.param.watch(handle_event, 'object')
42 7
    html._server_change(session.document, None, 'text', '<h1>Title</h1>', '<h1>New Title</h1>')
43

44

45 7
def test_server_static_dirs():
46 7
    html = Markdown('# Title')
47

48 7
    static = {'tests': os.path.dirname(__file__)}
49 7
    server = serve(html, port=5008, threaded=True, static_dirs=static, show=False)
50

51
    # Wait for server to start
52 7
    time.sleep(1)
53

54 7
    r = requests.get("http://localhost:5008/tests/test_server.py")
55

56 7
    try:
57 7
        with open(__file__, encoding='utf-8') as f:
58 7
            assert f.read() == r.content.decode('utf-8').replace('\r\n', '\n')
59
    finally:
60 7
        server.stop()
61

62

63 7
def test_server_async_callbacks():
64 7
    button = Button(name='Click')
65

66 7
    counts = []
67

68 7
    async def cb(event, count=[0]):
69 7
        import asyncio
70 7
        count[0] += 1
71 7
        counts.append(count[0])
72 7
        await asyncio.sleep(1)
73 7
        count[0] -= 1
74

75 7
    button.on_click(cb)
76

77 7
    server = serve(button, port=5008, threaded=True, show=False)
78

79
    # Wait for server to start
80 7
    time.sleep(1)
81

82 7
    requests.get("http://localhost:5008/")
83

84 7
    doc = list(button._models.values())[0][0].document
85 7
    with set_curdoc(doc):
86 7
        for _ in range(5):
87 7
            button.clicks += 1
88

89
    # Wait for callbacks to be scheduled
90 7
    time.sleep(2)
91

92
    # Ensure multiple callbacks started concurrently
93 7
    try:
94 7
        assert max(counts) > 1
95
    finally:
96 7
        server.stop()
97

98

99 7
def test_server_session_info():
100 7
    with config.set(session_history=-1):
101 7
        html = Markdown('# Title')
102

103 7
        server = serve(html, port=5009, threaded=True, show=False)
104

105
        # Wait for server to start
106 7
        time.sleep(1)
107

108 7
        requests.get("http://localhost:5009/")
109

110 7
        assert state.session_info['total'] == 1
111 7
        assert len(state.session_info['sessions']) == 1
112 7
        sid, session = list(state.session_info['sessions'].items())[0]
113 7
        assert session['user_agent'].startswith('python-requests')
114 7
        assert state.session_info['live'] == 0
115

116 7
        doc = list(html._documents.keys())[0]
117 7
        session_context = param.Parameterized()
118 7
        session_context._document = doc
119 7
        session_context.id = sid
120 7
        doc._session_context = session_context
121 7
        state.curdoc = doc
122 7
        state._init_session(None)
123 7
        assert state.session_info['live'] == 1
124

125 7
    server.stop()
126 7
    state.curdoc = None
127 7
    html._server_destroy(session_context)
128 7
    assert state.session_info['live'] == 0
129

130

131 7
def test_show_server_info(html_server_session, markdown_server_session):
132 7
    server_info = repr(state)
133 7
    assert "localhost:5006 - HTML" in server_info
134 7
    assert "localhost:5007 - Markdown" in server_info
135

136

137 7
def test_kill_all_servers(html_server_session, markdown_server_session):
138 7
    _, server_1, _ = html_server_session
139 7
    _, server_2, _ = markdown_server_session
140 7
    state.kill_all_servers()
141 7
    assert server_1._stopped
142 7
    assert server_2._stopped
143

144

145 7
def test_multiple_titles(multiple_apps_server_sessions):
146
    """Serve multiple apps with a title per app."""
147 7
    session1, session2 = multiple_apps_server_sessions(
148
        slugs=('app1', 'app2'), titles={'app1': 'APP1', 'app2': 'APP2'})
149 7
    assert session1.document.title == 'APP1'
150 7
    assert session2.document.title == 'APP2'
151

152
    # Slug names and title keys should match
153 7
    with pytest.raises(KeyError):
154 7
        session1, session2 = multiple_apps_server_sessions(
155
            slugs=('app1', 'app2'), titles={'badkey': 'APP1', 'app2': 'APP2'})

Read our documentation on viewing source code .

Loading