|
131 | 131 | { |
132 | 132 | "data": { |
133 | 133 | "text/plain": [ |
134 | | - "datetime.datetime(2024, 12, 3, 14, 0)" |
| 134 | + "datetime.datetime(2024, 12, 4, 14, 0)" |
135 | 135 | ] |
136 | 136 | }, |
137 | 137 | "execution_count": null, |
|
2423 | 2423 | "name": "stdout", |
2424 | 2424 | "output_type": "stream", |
2425 | 2425 | "text": [ |
2426 | | - "Set to 2024-12-03 13:51:41.200169\n" |
| 2426 | + "Set to 2024-12-04 09:20:09.428221\n" |
2427 | 2427 | ] |
2428 | 2428 | }, |
2429 | 2429 | { |
2430 | 2430 | "data": { |
2431 | 2431 | "text/plain": [ |
2432 | | - "'Session time: 2024-12-03 13:51:41.200169'" |
| 2432 | + "'Session time: 2024-12-04 09:20:09.428221'" |
2433 | 2433 | ] |
2434 | 2434 | }, |
2435 | 2435 | "execution_count": null, |
|
2637 | 2637 | " \"Add routes to an app\"\n", |
2638 | 2638 | " def __init__(self, prefix:str|None=None): \n", |
2639 | 2639 | " self.routes,self.wss = [],[]\n", |
2640 | | - " self.rt_funcs = RouteFuncs() # Store wrapped functions for discoverability\n", |
| 2640 | + " self.rt_funcs = RouteFuncs() # Store wrapped route function for discoverability\n", |
2641 | 2641 | " self.prefix = prefix if prefix else \"\"\n", |
2642 | 2642 | "\n", |
2643 | 2643 | " def _wrap_func(self, func, path=None):\n", |
2644 | 2644 | " name = func.__name__\n", |
2645 | | - " \n", |
2646 | | - " class _lf:\n", |
2647 | | - " def __init__(s): update_wrapper(s, func)\n", |
2648 | | - " def __call__(s, *args, **kw): return func(*args, **kw)\n", |
2649 | | - " def to(s, **kw): return qp(path, **kw)\n", |
2650 | | - " def __str__(s): return path\n", |
2651 | | - " \n", |
2652 | | - " wrapped = _lf()\n", |
| 2645 | + " wrapped = _mk_locfunc(func, path)\n", |
2653 | 2646 | " wrapped.__routename__ = name\n", |
2654 | 2647 | " # If you are using the def get or def post method names, this approach is not supported\n", |
2655 | 2648 | " if name not in all_meths: setattr(self.rt_funcs, name, wrapped)\n", |
|
2671 | 2664 | " \n", |
2672 | 2665 | " def ws(self, path:str, conn=None, disconn=None, name=None, middleware=None):\n", |
2673 | 2666 | " \"Add a websocket route at `path`\"\n", |
2674 | | - " def f(func=noop): return self.wss.append((func, path, conn, disconn, name, middleware))\n", |
| 2667 | + " def f(func=noop): return self.wss.append((func, f\"{self.prefix}{path}\", conn, disconn, name, middleware))\n", |
2675 | 2668 | " return f" |
2676 | 2669 | ] |
2677 | 2670 | }, |
|
2712 | 2705 | { |
2713 | 2706 | "cell_type": "code", |
2714 | 2707 | "execution_count": null, |
2715 | | - "id": "8c265ff8", |
| 2708 | + "id": "cd413b0d", |
2716 | 2709 | "metadata": {}, |
2717 | 2710 | "outputs": [], |
2718 | 2711 | "source": [ |
2719 | | - "assert str(ar.rt_funcs.index) == '/'\n", |
2720 | | - "assert str(yoyo) == '/yoyo'\n", |
2721 | | - "assert \"get\" not in ar.rt_funcs._funcs.keys()" |
| 2712 | + "app,cli,_ = get_cli(FastHTML())\n", |
| 2713 | + "ar.to_app(app)" |
2722 | 2714 | ] |
2723 | 2715 | }, |
2724 | 2716 | { |
2725 | 2717 | "cell_type": "code", |
2726 | 2718 | "execution_count": null, |
2727 | | - "id": "cd413b0d", |
| 2719 | + "id": "8c265ff8", |
2728 | 2720 | "metadata": {}, |
2729 | 2721 | "outputs": [], |
2730 | 2722 | "source": [ |
2731 | | - "app,cli,_ = get_cli(FastHTML())\n", |
2732 | | - "ar.to_app(app)" |
| 2723 | + "assert str(ar.rt_funcs.index) == '/'\n", |
| 2724 | + "assert str(yoyo) == '/yoyo'\n", |
| 2725 | + "assert \"get\" not in ar.rt_funcs._funcs.keys()" |
2733 | 2726 | ] |
2734 | 2727 | }, |
2735 | 2728 | { |
|
2806 | 2799 | "ar2.to_app(app)" |
2807 | 2800 | ] |
2808 | 2801 | }, |
| 2802 | + { |
| 2803 | + "cell_type": "code", |
| 2804 | + "execution_count": null, |
| 2805 | + "id": "f1fc8425", |
| 2806 | + "metadata": {}, |
| 2807 | + "outputs": [], |
| 2808 | + "source": [ |
| 2809 | + "assert str(ar2.rt_funcs.index) == '/products/'\n", |
| 2810 | + "assert str(yoyo) == '/products/yoyo'\n", |
| 2811 | + "assert \"get\" not in ar2.rt_funcs._funcs.keys()" |
| 2812 | + ] |
| 2813 | + }, |
2809 | 2814 | { |
2810 | 2815 | "cell_type": "code", |
2811 | 2816 | "execution_count": null, |
|
2825 | 2830 | { |
2826 | 2831 | "cell_type": "code", |
2827 | 2832 | "execution_count": null, |
2828 | | - "id": "f1fc8425", |
| 2833 | + "id": "e38d99cf", |
2829 | 2834 | "metadata": {}, |
2830 | 2835 | "outputs": [], |
2831 | 2836 | "source": [ |
2832 | | - "assert str(ar2.rt_funcs.index) == '/products/'\n", |
2833 | | - "assert str(yoyo) == '/products/yoyo'\n", |
2834 | | - "assert \"get\" not in ar2.rt_funcs._funcs.keys()" |
| 2837 | + "with cli.websocket_connect('/products/ws') as ws:\n", |
| 2838 | + " ws.send_text('{\"msg\":\"Hi!\"}')\n", |
| 2839 | + " data = ws.receive_text()\n", |
| 2840 | + " assert data == 'Message text was: Hi!'" |
2835 | 2841 | ] |
2836 | 2842 | }, |
2837 | 2843 | { |
|
2927 | 2933 | { |
2928 | 2934 | "data": { |
2929 | 2935 | "text/plain": [ |
2930 | | - "'Cookie was set at time 13:51:41.310412'" |
| 2936 | + "'Cookie was set at time 09:20:09.542569'" |
2931 | 2937 | ] |
2932 | 2938 | }, |
2933 | 2939 | "execution_count": null, |
|
0 commit comments