Skip to content

Commit

Permalink
update put_markdown() usage
Browse files Browse the repository at this point in the history
  • Loading branch information
wang0618 committed Nov 19, 2021
1 parent ea5b8dd commit c7f4c5f
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 34 deletions.
2 changes: 1 addition & 1 deletion demos/bmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def main():
## BMI指数计算器
本程序的源代码[链接](https://github.com/wang0618/PyWebIO/blob/dev/demos/bmi.py)
"""), strip_indent=4)
"""))

info = input_group(t('BMI calculation', '计算BMI:'), [
input(t("Your Height(cm)", "请输入你的身高(cm)"), name="height", type=FLOAT),
Expand Down
4 changes: 2 additions & 2 deletions demos/bokeh_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def main():
在PyWebIO中,你也可以使用 `bokeh.io.show()` 来显示一个Bokeh App,和输出普通图表一样,只需要在会话开始时调用 `bokeh.io.output_notebook(notebook_type='pywebio')` 来设置PyWebIO输出环境。
以下为一个 Bokeh App demo:
""", lstrip=True)
""")
else:
put_markdown("""# Bokeh Applications in PyWebIO
[Bokeh Applications](https://docs.bokeh.org/en/latest/docs/user_guide/server.html) can be built by starting the Bokeh server. The purpose of the Bokeh server is to make it easy for Python users to create interactive web applications that can connect front-end UI events to real, running Python code.
Expand All @@ -52,7 +52,7 @@ def main():
You can use `bokeh.io.output_notebook(notebook_type='pywebio')` in the PyWebIO session to setup Bokeh environment. Then you can use `bokeh.io.show()` to output a boken application.
This is a demo of Bokeh App:
""", lstrip=True)
""")

show(bkapp)

Expand Down
2 changes: 1 addition & 1 deletion demos/chat_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def main():
"""
global chat_msgs

put_markdown(t("## PyWebIO chat room\nWelcome to the chat room, you can chat with all the people currently online. You can open this page in multiple tabs of your browser to simulate a multi-user environment. This application uses less than 90 lines of code, the source code is [here](https://github.com/wang0618/PyWebIO/blob/dev/demos/chat_room.py)", "## PyWebIO聊天室\n欢迎来到聊天室,你可以和当前所有在线的人聊天。你可以在浏览器的多个标签页中打开本页面来测试聊天效果。本应用使用不到90行代码实现,源代码[链接](https://github.com/wang0618/PyWebIO/blob/dev/demos/chat_room.py)"), lstrip=True)
put_markdown(t("## PyWebIO chat room\nWelcome to the chat room, you can chat with all the people currently online. You can open this page in multiple tabs of your browser to simulate a multi-user environment. This application uses less than 90 lines of code, the source code is [here](https://github.com/wang0618/PyWebIO/blob/dev/demos/chat_room.py)", "## PyWebIO聊天室\n欢迎来到聊天室,你可以和当前所有在线的人聊天。你可以在浏览器的多个标签页中打开本页面来测试聊天效果。本应用使用不到90行代码实现,源代码[链接](https://github.com/wang0618/PyWebIO/blob/dev/demos/chat_room.py)"))

msg_box = output()
put_scrollable(msg_box, height=300, keep_bottom=True)
Expand Down
2 changes: 1 addition & 1 deletion demos/gomoku_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def player_exit():
This application uses less than 100 lines of code, the source code is [here](https://github.com/wang0618/PyWebIO/blob/dev/demos/gomoku_game.py)
Currently online player: {player_count[0]} for ⚫, {player_count[1]} for ⚪.
Your role is {my_chess}.
""", lstrip=True)
""")

def set_stone(pos):
global current_turn
Expand Down
18 changes: 9 additions & 9 deletions demos/input_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def main():
```python
name = input("What's your name?")
```
"""), lstrip=True)
"""))
put_text(t("The results of the above example are as follows:", "这样一行代码的效果如下:"))
name = input("What's your name?")
put_markdown("`name = %r`" % name)
Expand Down Expand Up @@ -95,7 +95,7 @@ def main():
# 文件上传
img = file_upload("Select a image:", accept="image/*")
```
"""), lstrip=True)
"""))
password = input("Input password", type=PASSWORD)
put_markdown("`password = %r`" % password)
gift = select('Which gift you want?', ['keyboard', 'ipad'])
Expand All @@ -117,14 +117,14 @@ def main():
There are many parameters that can be passed to the input function:
""", """#### 输入选项
输入函数可指定的参数非常丰富:
"""), strip_indent=4)
"""))
put_markdown("""
```python
input('This is label', type=TEXT, placeholder='This is placeholder',
help_text='This is help text', required=True,
datalist=['candidate1', 'candidate2', 'candidate2'])
```
""", strip_indent=4)
""")
input('This is label', type=TEXT, placeholder='This is placeholder',
help_text='This is help text', required=True,
datalist=['candidate1', 'candidate2', 'candidate2'])
Expand All @@ -141,7 +141,7 @@ def check_age(p): # return None when the check passes, otherwise return the err
age = input("How old are you?", type=NUMBER, validate=check_age)
```
""", strip_indent=4)
""")

def check_age(p): # 检验函数校验通过时返回None,否则返回错误消息
if p < 10:
Expand All @@ -161,7 +161,7 @@ def check_age(p): # 检验函数校验通过时返回None,否则返回错误
'theme': 'darcula', # Codemirror theme
}, value='import something\n# Write your python code')
```
""", strip_indent=4)
""")

code = textarea('Code Edit', code={
'mode': "python", # 编辑区代码语言
Expand All @@ -176,7 +176,7 @@ def check_age(p): # 检验函数校验通过时返回None,否则返回错误
The input group also supports using `validate` parameter to set the validation function, which accepts the entire form data as parameter:""",
"""### 输入组
`input_group()` 接受单项输入组成的列表作为参数,输入组中需要在每一项输入函数中提供 `name` 参数来用于在结果中标识不同输入项。输入组中同样支持设置校验函数,其接受整个表单数据作为参数。检验函数校验通过时返回None,否则返回 `(input name,错误消息)`
"""), strip_indent=4)
"""))
put_markdown(r"""
```python
def check_form(data): # input group validation: return (input name, error msg) when validation fail
Expand All @@ -190,7 +190,7 @@ def check_form(data): # input group validation: return (input name, error msg)
input('Input your age', name='age', type=NUMBER, validate=check_age)
], validate=check_form)
```
""", strip_indent=4)
""")

def check_form(data): # input group validation: return (input name, error msg) when validation fail
if len(data['name']) > 6:
Expand All @@ -209,7 +209,7 @@ def check_form(data): # input group validation: return (input name, error msg)
For more information about input of PyWebIO, please visit PyWebIO [User Guide](https://pywebio.readthedocs.io/zh_CN/latest/guide.html) and [input module documentation](https://pywebio.readthedocs.io/zh_CN/latest/input.html).
""", """----
PyWebIO的输入演示到这里就结束了,更多内容请访问PyWebIO[用户指南](https://pywebio.readthedocs.io/zh_CN/latest/guide.html)和[input模块文档](https://pywebio.readthedocs.io/zh_CN/latest/input.html)。
"""), lstrip=True)
"""))


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion demos/markdown_previewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def main():
put_markdown("""# Markdown Live Preview
The online markdown editor with live preview. The source code of this application is [here](https://github.com/wang0618/PyWebIO/blob/dev/demos/markdown_previewer.py).
## Write your Markdown
""", lstrip=True)
""")
put_textarea('md_text', rows=18, code={'mode': 'markdown'})

put_buttons(['Download content'], lambda _: download('saved.md', pin.md_text.encode('utf8')), small=True)
Expand Down
24 changes: 12 additions & 12 deletions demos/output_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def main():
### 基本输出
PyWebIO提供了一些便捷函数来输出表格、链接等格式:
"""), strip_indent=4)
"""))

code_block(t(r"""
# Text Output
Expand Down Expand Up @@ -101,7 +101,7 @@ async def main():
函数名以 `put_` 开始的输出函数,可以与一些输出函数组合使用,作为最终输出的一部分。
比如`put_table()`支持以`put_xxx()`调用作为单元格内容:
"""), strip_indent=4)
"""))

code_block(r"""
put_table([
Expand All @@ -116,7 +116,7 @@ async def main():
""")

put_markdown(t(r"Similarly, you can pass `put_xxx()` calls to `popup()` as the popup content:",
r"类似地,`popup()`也可以将`put_xxx()`调用作为弹窗内容:"), strip_indent=4)
r"类似地,`popup()`也可以将`put_xxx()`调用作为弹窗内容:"))

code_block(r"""
popup('Popup title', [
Expand Down Expand Up @@ -154,7 +154,7 @@ def edit_row(choice, row):
[3, put_buttons(['edit', 'delete'], onclick=partial(edit_row, row=3))],
])
```
""", strip_indent=4)
""")

from functools import partial

Expand All @@ -179,7 +179,7 @@ def btn_click(btn_val):
put_button("Click me", onclick=lambda: toast("Clicked")) # single button
```
""", strip_indent=4)
""")

@use_scope('button-callback')
def btn_click(btn_val):
Expand All @@ -199,7 +199,7 @@ def btn_click(btn_val):
['Apple', put_text('5.5').onclick(lambda: toast('You click the text'))],
])
```
""", strip_indent=4)
""")

put_image('https://www.python.org/static/img/python-logo.png').onclick(lambda: toast('You click the image'))
# set onclick in combined output
Expand Down Expand Up @@ -242,7 +242,7 @@ def btn_click(btn_val):
with use_scope('C'):
put_text('Text in scope C')
```
""", strip_indent=4)
""")
with use_scope('A'):
put_text('Text in scope A')

Expand All @@ -265,7 +265,7 @@ def btn_click(btn_val):
输出函数(函数名形如 `put_xxx()` )在默认情况下,会将内容输出到”当前Scope”,可以通过 `use_scope()` 设置运行时上下文的”当前Scope”。
此外,也可以通过输出函数的 scope 参数指定输出的目的Scope:
"""), strip_indent=4)
"""))

put_grid([
[put_code("put_text('A', scope='A')", 'python'), None, put_buttons([t('Run', '运行')], [lambda: put_text('A', scope='A')])],
Expand All @@ -277,7 +277,7 @@ def btn_click(btn_val):
```python
put_text(now(), scope='A', position=...)
```
""", strip_indent=4)
""")
import datetime

put_buttons([('position=%s' % i, i) for i in [1, 2, 3, -1, -2, -3]],
Expand Down Expand Up @@ -313,7 +313,7 @@ def btn_click(btn_val):
- `put_grid()` : 使用网格布局输出内容
比如,通过通过组合 `put_row()` 和 `put_column()` 实现的布局:
"""), strip_indent=4)
"""))

code_block(r"""
put_row([
Expand All @@ -340,7 +340,7 @@ def btn_click(btn_val):
如果你熟悉 CSS样式 ,你还可以使用 `style()` 方法给输出设定自定义样式。
可以给单个的 `put_xxx()` 输出设定CSS样式,也可以配合组合输出使用:
"""), strip_indent=4)
"""))

code_block(r"""
put_text('Red').style('color: red')
Expand All @@ -355,7 +355,7 @@ def btn_click(btn_val):
For more information about output of PyWebIO, please visit PyWebIO [User Guide](https://pywebio.readthedocs.io/zh_CN/latest/guide.html) and [output module documentation](https://pywebio.readthedocs.io/zh_CN/latest/output.html).
""", """----
PyWebIO的输出演示到这里就结束了,更多内容请访问PyWebIO[用户指南](https://pywebio.readthedocs.io/zh_CN/latest/guide.html)和[output模块文档](https://pywebio.readthedocs.io/zh_CN/latest/output.html)。
"""), lstrip=True)
"""))


if __name__ == '__main__':
Expand Down
24 changes: 19 additions & 5 deletions demos/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def output_widgets():
> We're living the future so
> the present is our past.
""", strip_indent=8),
"""),

put_markdown("""
### Lists
Expand All @@ -145,7 +145,7 @@ def output_widgets():
- [x] list syntax required (any unordered or ordered list supported)
- [x] this is a complete item
- [ ] this is an incomplete item
""", strip_indent=8)
""")
])

###########################################################################################
Expand All @@ -160,7 +160,7 @@ def main(): # PyWebIO application function
start_server(main, port=8080, debug=True)
```
""", strip_indent=4)
""")
###########################################################################################
put_markdown('# Image')
with use_scope('image'):
Expand Down Expand Up @@ -203,7 +203,7 @@ def main(): # PyWebIO application function
------------ | -------------
Content from cell 1 | Content from cell 2
Content in the first column | Content in the second column
""", strip_indent=4)
""")

put_table([
['Type', 'Content'],
Expand Down Expand Up @@ -357,12 +357,26 @@ def page():
else:
put_table([themes])

if theme != 'default':
put_markdown(f"""
### Usage
Use `pywebio.config()` to apply this theme:
```python
@config(theme="{theme}")
def main():
put_text("hello world")
start_server(main, port=8080)
```
""")

put_markdown("""
### Credits
The dark theme is modified from ForEvolve's [bootstrap-dark](https://github.com/ForEvolve/bootstrap-dark).
The sketchy, minty and yeti theme are from [bootswatch](https://bootswatch.com/4/).
""", lstrip=True)
""")

set_env(input_panel_min_height=100, input_panel_init_height=190)
output_widgets()
Expand Down
4 changes: 2 additions & 2 deletions test/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def basic_output():
[链接](./#)
~~删除线~~
""", lstrip=True)
""")

put_link('链接', '#')

Expand Down Expand Up @@ -243,7 +243,7 @@ def edit_row(choice, row):
* `user_ip` (str): {session_info.user_ip}
* `request type check` (str): {request_ok}
```
""", strip_indent=4)
""")

put_markdown('### Layout')
put_row([
Expand Down

0 comments on commit c7f4c5f

Please sign in to comment.