pywebio / PyWebIO

Write interactive web app in script way.
https://pywebio.readthedocs.io
MIT License
4.54k stars 383 forks source link

[Feature Request] start_scriptmode server function #39

Closed KnorrFG closed 3 years ago

KnorrFG commented 3 years ago

Hey, pywebio is an awesome idea, thank you for that :)

Thanks to corona im regularly working remotely over ssh, and when I want to toy around with data, I usually generate html files that contain plots and tables, copy them to my local machine, and open them here (sshfs has some quirks inside wsl2). I had hoped to be able to use pywebio instead, but when run in scripting mode, the server is always opened on another port. I thought, maybe I could just look at the port, in which it opens using the session.info, and then create a local port forwarding (so there would be two forwardings: my machine:8080 -> server:8080 and server:8080 -> wherever pywebio runs), im not sure, whether that would have worked, but I couldn't try, because pywebio would always open a browser on the server (lynx or w3m since no X), and then it would freeze on the first put_ function, probably because they rely on some feature the browser doesn't support. After uninstalling lynx and w3m, nothing changed. So what would be awesome for me, would be a function start_script_mode_server(port=8080, openBrowser=False) I first tried to add it myself, but the code base is too complex to understand it quickly (and web is not really my forte), and I can't read Chinese, which a lot of comments are in. If you would be willing to add this feature, that would be awesome.

PS: sorry, I dont know how to remove the bug label

wang0618 commented 3 years ago

I think what you need is the server mode of PyWebIO, see https://pywebio.readthedocs.io/en/latest/guide.html#server-mode-and-script-mode for details.

KnorrFG commented 3 years ago

But the server requires that you pass it a function, I actually want to use pywebio interactively. Show a plot, think about it, do something with the data, show a new plot, repeat. Thats not possible with the server, isnt it?

wang0618 commented 3 years ago

It's possible with the server. You need to wrap the operation you need into a function, and then use start server to deploy it to web.

KnorrFG commented 3 years ago

So I would call start server over and over again? Also, as far as i understand, the server keeps running indefinitely, so how would I call it a second time?

wang0618 commented 3 years ago

I realized that I may have misunderstood you before. If you want to manually specify the script mode port and disable the automatic opening of the browser, you can use the PYWEBIO_SCRIPT_MODE_PORT environment variable when running script.

For example: PYWEBIO_SCRIPT_MODE_PORT=8080 python myscript.py

Or you can set this environment variable at the beginning of you script:

import os
os.environ["PYWEBIO_SCRIPT_MODE_PORT"] = "8080"
KnorrFG commented 3 years ago

This was exactly what I needed, ty. However, I can access the pywebio server from my local pc now, and the input() function works, the popup() function does too, but the put_text() function does not. I see its return value in the console, and there is no error, but no text is showing up. Do you have an idea why?

this is my console:

  [ins] In [1]: import os

  [ins] In [2]: os.environ["PYWEBIO_SCRIPT_MODE_PORT"] = "8888"

  [ins] In [3]: import pywebio

  [ins] In [4]: import pywebio.input as wi

  [ins] In [5]: import pywebio.output as wo

  [ins] In [6]: wo.put_text("test")                                                     
 Out[6]: <pywebio.io_ctrl.Output at 0x7fb27db49df0>              

 [ins] In [7]:

I do reach the server in my browser, but the page is empty

wang0618 commented 3 years ago

Yes, beacuse the python console will cache the result of function calls, and this will causes the output fail to flush to the user. If you call output functions in python console, you need manually call send() method on the return value of output:

>>> from pywebio.output import *
>>> put_text('test').send()
KnorrFG commented 3 years ago

Awesome. Thanks a lot :) Thanks to your awesome library, and your help, my workflow got quite a bit smoother 🥳