首页 文章 精选 留言 我的

精选列表

搜索[网站开发],共10000篇文章
优秀的个人博客,低调大师

基于hi-nginx的web开发(python篇)——表单处理和文件上传

hi-nginx会自动处理表单,所以,在hi.py框架里,要做的就是直接使用这些数据。 表单数据一般用GET和POST方法提交。hi-nginx会把这些数据解析出来,放在form成员变量里。对python来说,要做的就是使用has_form和get_form方法取出想要的数据。 例如路由如下: @app.route('^/form/?$',['GET','POST']) def form(req,res,param): name='None' if req.has_form('name'): name=req.get_form('name') res.content('{}={}'.format('name',name)) res.status(200) 上面的路由表示可以使用get和post两种方法向/form提交名为name的数据。可以用curl测试下: curl -i http://localhost:8080/form?name=123 或者 curl -i -d 'name=123' http://localhost:8080/form hi.py处理表单的方式就是这么直接简单。 以上路由还可以直接处理文件上传,只需把name理解为上传文件的临时地址就可以了: echo 'test upload file' > name.file curl -i -F'name=@name.file' http://localhost:8080/form 上图表示当我们把文件name.file上传给/form时,该文件会被暂时保存在hi-nginx安装目录下的temp目录中,文件名为temp/94ffcfe29b03c0c7368a2fdd842d54ca.file。因此,我们要做的仅仅是把该文件移动到想要的地址去就行了。 因为文件有大小,服务器也会对文件大小做出限制,所以最好是使用nginx的client_max_body_size,比如 client_max_body_size 100k; 命令限制文件上传的大小限制。

优秀的个人博客,低调大师

基于hi-nginx的web开发(python篇)——动态路由和请求方法

hi.py 的提供的路由装饰器接受两个参数,第一个参数指定动态路由的正则模式,第二个参数指定同意的http请求方法列表。 比如: 1 @app.route(r"^/client/?$",['GET','POST']) 2 def client(req,res,param): 3 res.content('{}<br>{}<br>{}<br>{}<br>{}'.format(req.client(),req.method(),req.uri(),req.user_agent(),req.param())) 4 res.status(200) 这个路由指定uri为/client或者/client/,同时请求方法为GET或者POST的http请求由函数client(req,res,param)来处理。uri模式由正则表达式构成,请求方法参数由一个list表示,可接受的方法名包括:GET,POST,PUT,HEAD,PATCH等,凡nginx能理解的http方法名均可。 处理函数有三个参数,分别是req,res和param。它们分别是hi_req,hi_res和由正则表达式引出的group dict。前两者是有hi-nginx提供的api,它们提供一系列方法来操控http协议: hi_req uri method client param user_agent has_header get_header has_form get_form has_session get_session has_cookie get_cookie hi_res status content header session 处理函数的第三个参数param可用来解析由正则模式提供的数据,比如: 1 @app.route(r"^/hello/(?P<who>\w+)?$",['GET']) 2 def hello(req,res,param): 3 res.content('{}={}'.format('who',param['who'])) 4 res.status(200) 正则模式^/hello/(?P<who>\w+)?$ 是python re模块可以理解的模式,当uri为/hello/cnblogs时,其中的参数param就会是一个包含以who为键,以cnblogs为其值的dict字典。因此,它可以用来为操作函数提供表单以外的数据,还能美化uri。比如/hello?who=cnblogs就不如/hello/cnblogs美观简洁。 另外,同一个操作函数也可以对应多个路由规则,比如: 1 @app.route(r'^/test/?$',['POST']) 2 @app.route(r"^/$",['GET']) 3 def hello_world(req,res,param): 4 res.header('Content-Type','text/plain;charset=utf-8') 5 res.content('hello,world') 6 res.status(200) 在上面的代码中,操作函数hello_world在uri为/或者/test或者/test/时均能被调用,前提是一个要求请求方法是GET,另一个则要求是POST方法。这种一个函数对应多个路由的功能消除了写下面这种代码的麻烦: 1 if req.method()=='GET': 2 ... 3 if req.method()=='POST': 4 ...

优秀的个人博客,低调大师

使用Dash开发交互式数据可视化网页--页面布局

Dash应用布局 后续的操作前,需要安装如下Python包 pip install dash==0.20.0 # The core dash backend pip install dash-renderer==0.11.2 # The dash front-end pip install dash-html-components==0.8.0 # HTML components pip install dash-core-components==0.18.1 # Supercharged components pip install plotly --upgrade # Plotly graphing library used in examples 使用Dash生成HTML Dash应用包括两个部分,应用布局(layout)和数据交互(interactivity)。其中布局部分用来展示数据以及引导使用者使用。Dash提供了dash_core_components和dash_html_components, 以类的方式对HTML和JS进行封装,便于调用。下面先构建一个最简单的布局 import dash import dash_core_components as dcc import dash_html_components as html app = dash.Dash() app.layout = html.Div(children=[ html.H1(children = 'Hello Dash'), html.Div(children = ''' Dash: A web application frameworkd for Python. '''), dcc.Graph( id = 'example-graph', figure = { 'dash':[ {'x': [1,2,3], 'y':[4,1,2], 'type':'bar', 'name':'SF'}, {'x': [1,2,3], 'y':[2,4,5], 'type':'bar', 'name':'Montrel'}, ], 'layout':{ 'title':'Dash data Visualization' } } ) ]) if __name__ == '__main__': app.run_server(debug=True, host='0.0.0.0') 首先用app=dash.Dash()创建了Dash应用的实例,这个实例可以通过app.run_server()运行。 其次这个应用的布局(layout)由html组件(html.Div等)和图形组件(dcc.Graph等)构成。其中基础的html标签来自于dash_html_components,而更加React.js库里的高级组件则是由dash_core_components提供。 最后的展示形式需要后期慢慢的调整, 比如说调整一下字体对齐, 字体颜色和背景颜色等 import dash_core_components as dcc import dash_html_components as html app = dash.Dash() colors = { 'background':'#111111', 'text':'#7FDBFF' } app.layout = html.Div(style={'backgroundColor':colors['background']}, children=[ html.H1( children = 'Hello Dash', style = { 'textAlign':'center', 'color': colors['text'] } ), html.Div(children = ''' Dash: A web application frameworkd for Python. ''', style = { 'textAlign':'center', 'color': colors['text'] } ), dcc.Graph( id = 'example-graph', figure = { 'data':[ {'x': [1,2,3], 'y':[4,1,2], 'type':'bar', 'name':'SF'}, {'x': [1,2,3], 'y':[2,4,5], 'type':'bar', 'name':'Montreal'}, ], 'layout':{ 'plot_bgcolor': colors['background'], 'paper_bgcolor': colors['background'], 'font':{ 'color': colors['text'] }, 'title':'Dash data Visualization' } } ) ]) if __name__ == '__main__': app.run_server(debug=True, host='0.0.0.0') 这里的html组件都设置了style,用来调整样式, 可视化 dash_core_components库中有一个Graph组件,它利用开源的JavaScript图形库--plotly.js进行交互式数据渲染。Graph里的figure参数等价于plotly.py里的figure参数,即任何plotly.js支持的图形都可以用dash_core_components调用。查看https://plot.ly/python/了解更多plotly.py的图形。 比如说这里可以基于Pandas的数据库创建散点图 import dash import dash_core_components as dcc import dash_html_components as html import plotly.graph_objs as go import pandas as pd app = dash.Dash() df = pd.read_csv( 'https://gist.githubusercontent.com/chriddyp/' + '5d1ea79569ed194d432e56108a04d188/raw/' + 'a9f9e8076b837d541398e999dcbac2b2826a81f8/'+ 'gdp-life-exp-2007.csv') plot = [dcc.Graph( id = 'life-exp-vs-GDP', figure = { 'data':[ go.Scatter( x=df[df['continent'] == i]['gdp per capita'], y=df[df['continent'] == i]['life expectancy'], text=df[df['continent'] == i]['country'], mode='markers', opacity=0.7, marker={ 'size':15, 'line':{'width':0.5, 'color':'white'} }, name = i ) for i in df.continent.unique() ], 'layout': go.Layout( xaxis={'type':'log','title':'GDP per Capita'}, yaxis={'title':'Life Expectancy'}, margin={'l':40,'b':40,'t':10,'r':10}, legend={'x':0, 'y':1}, hovermode='closest' ) } )] app.layout = html.Div( html.Div(children=[ html.Div(className='col-md-4'), html.Div(plot,className='col-md-4')], className='row' ) ) # Append an externally hosted CSS stylesheet my_css_url = "https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" app.css.append_css({ "external_url": my_css_url }) # Append an externally hosted JS bundle my_js_url = 'https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js' app.scripts.append_script({ "external_url": my_js_url }) if __name__ == '__main__': app.run_server(debug=True) 这部分代码将图形部分的代码从html组件中抽离出来,写完之后,再添加到html总体组件中。此外还增加了bootstrap的css样式。 Markdown语法 Dash的dash_html_components支持原生的HTML语句,而dash_core_components得Markdown提供了Markdown得渲染。 import dash import dash_core_components as dcc import dash_html_components as html app = dash.Dash() markdown_text = ''' ### Dash and Markdown Dash apps can be written in Markdown. Dash uses the [CommonMark](http://commonmark.org/) specification of Markdown. Check out their [60 Second Markdown Tutorial](http://commonmark.org/help/) if this is your first introduction to Markdown! ''' app.layout = html.Div([ dcc.Markdown(children=markdown_text) ]) if __name__ == '__main__': app.run_server(debug=True) dash_core_components里不仅仅提供了Markdown, graphs这些图形组件,还支持下拉栏等其他使用工具,可在https://plot.ly/dash/dash-core-components进一步了解 小节 这部分主要是学习了Dash应用得layout. layout是不同组件的层级关系树,最后结果是html页面。html页面的HTML基本语法由dash_html_components提供,而高级的绘图和下拉栏等则是由dash_core_components提供. 参考资料: https://plot.ly/dash/getting-started https://plot.ly/dash/dash-core-components https://plot.ly/dash/dash-html-components

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册