首页 文章 精选 留言 我的

精选列表

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

基于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

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

开发者笔记】python中的类方法(@classmethod)和静态方法(@staticmethod)

在java、c#等高级语言中我们用static来定义静态方法和静态变量,那么在python中如何定义静态方法和静态变量呢。 python提供了@classmethod和@staticmethod来定义静态方法,刚接触的时候不太明白,Stack Overflow提供了一个比较方便理解的解释,Stack Overflow回答。 但是看完还是不太理解,于是自己写了个实例: class stclass(): d=1 #对象方法 def imethod(self): print(self) print("instance method") #类方法 @classmethod def cmethod(cls): print(cls) print("class method") #静态方法 @staticmethod def smethod(): print("static method") sc = stclass() sc.imethod() sc.cmethod() sc.smethod() 运行结果如下: 然后就可以解释了: 1、实例方法,该实例属于对象,该方法的第一个参数是当前实例,拥有当前类以及实例的所有特性。 2、类方法,该实例属于类,该方法的第一个参数是当前类,可以对类做一些处理,如果一个静态方法和类有关但是和实例无关,那么使用该方法。 3、静态方法,该实例属于类,但该方法没有参数,也就是说该方法不能对类做处理,相当于全局方法。 黑夜给了我黑色的眼睛,我却用它寻找光明

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

iOS开发之iPhone通过get和post方式请求asp.net webservice

这篇文章,我将通过一个简单的例子来展现iPhone通过get和post方式请求asp.net webservice。 webservice 1、创建一个webservice 2、在webconfig中启用http get 和http post。 < webServices > < protocols > < add name ="HttpSoap" /> < add name ="HttpPost" /> < add name ="HttpGet" /> < add name ="Documentation" /> </ protocols > </ webServices > iphone客户端调用: 1、get的方式: NSString * queryString = [NSStringstringWithFormat: @“http: // 10.5.23.117:5111/Service1.asmx/HelloWorld?param=123%@“, ipAddress.text]; NSURL * url = [NSURLURLWithString:queryString]; NSMutableURLRequest * req = [NSMutableURLRequestrequestWithURL:url]; [reqaddValue:@“text / xml;charset = utf - 8 ”forHTTPHeaderField:@“Content - Type”]; [reqaddValue: 0 forHTTPHeaderField:@“Content - Length”]; [reqsetHTTPMethod:@“GET”]; [activityIndicatorstartAnimating]; conn = [[NSURLConnectionalloc]initWithRequest:req delegate :self]; if (conn){ webData = [[NSMutableDatadata]retain]; } 2、post的方式: NSString *postString =@ "123" ; NSURL *url = [ NSURL URLWithString: @“http: //10.5.23.117:5111/Service1.asmx/HelloWorld2”]; NSMutableURLRequest *req = [ NSMutableURLRequest requestWithURL:url]; NSString *msgLength = [ NSString stringWithFormat:@“%d”, [postString length]]; [req addValue:@“application/x-www-form-urlencoded” forHTTPHeaderField:@“Content-Type”]; [req addValue:msgLength forHTTPHeaderField:@“Content-Length”]; [req setHTTPMethod:@“POST”]; [req setHTTPBody: [postString dataUsingEncoding: NSUTF8StringEncoding ]]; [activityIndicator startAnimating]; conn = [[ NSURLConnection alloc] initWithRequest:req delegate: self ]; if (conn) { webData = [[ NSMutableData data] retain]; } 本文转自麒麟博客园博客,原文链接:http://www.cnblogs.com/zhuqil/archive/2011/07/13/2105640.html,如需转载请自行联系原作者

资源下载

更多资源
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应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册