web.py

How do I use python for web development without relying on a framework?

自作多情 提交于 2019-11-29 19:45:05
I know the various frameworks have their benefits, but I personally want my web development in python to be as straight-forward as possible: less writing to the framework, more writing python . The only thing I have found so far that lets me do this in the most obvious way possible is web.py but I have slight concerns on its performance. For those of you using nginx(or another flavour)+mod_wsgi+web.py... how's performance? Can it be improved further? For those of you who have used web.py, liked the idea and went on to write something better or found something better... care to point me to the

Changing the static directory path in webpy

别等时光非礼了梦想. 提交于 2019-11-29 05:03:35
问题 I'd love to be able to change the webpy static directory without the need to set up and run nginx locally. Right now, it seems webpy will only create a static directory if /static/ exists. In my case, I want to use /foo/bar/ as my static directory, but couldn't find any info related to configuring this (other than running apache or nginx locally). This is for local use only, not production. Any ideas? Thanks 回答1: If you need to have different directory for the same path then you may subclass

webpy: How to serve JSON

为君一笑 提交于 2019-11-28 17:55:42
Is it possible to use webpy to serve JSON? I built my website and I need to serve some information in JSON to interact with the Javascript on some pages. I try to look for answers in the documentation, but I'm not able to find anything. Thanks, Giovanni I wouldn't think you'd have to do any thing overly "special" for web.py to serve JSON. import web import json class index: def GET(self): pyDict = {'one':1,'two':2} web.header('Content-Type', 'application/json') return json.dumps(pyDict) It is certainly possible to serve JSON from webpy, But if you and choosing a framework, I would look at

Rendering newlines in user-submitted content (Python web app)

限于喜欢 提交于 2019-11-28 14:07:34
I have a web.py app that takes input from a textarea and inputs it to a database. I can get the information from the database and post it to the page but the NEWLINES are gone. How do I preserver newlines when posting back into HTML? The data does have \r\n in it but that isn't rendered as NEWLINES in HTML. Any thoughts? Here is a small example: (2, u'Title', u'content here...hey\r\nthis\r\nhas\r\nbreaks in it....?', datetime.datetime(2012, 7, 5, 21, 5, 14, 354516)) That is my return from the data base. I need the \r\n to represent a <br /> and if there is two a <p> would be awesome. Any

Rendering newlines in user-submitted content (Python web app)

情到浓时终转凉″ 提交于 2019-11-27 19:31:38
问题 I have a web.py app that takes input from a textarea and inputs it to a database. I can get the information from the database and post it to the page but the NEWLINES are gone. How do I preserver newlines when posting back into HTML? The data does have \r\n in it but that isn't rendered as NEWLINES in HTML. Any thoughts? Here is a small example: (2, u'Title', u'content here...hey\r\nthis\r\nhas\r\nbreaks in it....?', datetime.datetime(2012, 7, 5, 21, 5, 14, 354516)) That is my return from the

web.py - specify address and port

梦想与她 提交于 2019-11-27 15:58:48
问题 How to specify listening address and port in web.py? Something like: web.application( urls, host="33.44.55.66", port=8080 ) Edit I would like to avoid using the default web.py command line parsing 回答1: From API docmentation of web.py: module web.httpserver function runsimple(func,server_address=('0.0.0.0', 8080)) Runs CherryPy WSGI server hosting WSGI app func. The directory static/ is hosted statically. Example code import web class MyApplication(web.application): def run(self, port=8080,

webpy: How to serve JSON

元气小坏坏 提交于 2019-11-27 10:54:31
问题 Is it possible to use webpy to serve JSON? I built my website and I need to serve some information in JSON to interact with the Javascript on some pages. I try to look for answers in the documentation, but I'm not able to find anything. Thanks, Giovanni 回答1: I wouldn't think you'd have to do any thing overly "special" for web.py to serve JSON. import web import json class index: def GET(self): pyDict = {'one':1,'two':2} web.header('Content-Type', 'application/json') return json.dumps(pyDict)

web.py开发web 第三章 Sqlalchemy

≯℡__Kan透↙ 提交于 2019-11-27 02:44:20
web开发中一般的主要版块就是视图、模板、数据库操作,前面已经讲了最基础的视图跟模板,这一章主要是介绍下数据库操作,如第一章前言所说,我们的数据库操作是使用sqlalchemy来实现的。 不同于很多web开发,python开发web的数据库很多是通过建立数据表类,绑定数据库连接,然后自动创建数据表,这种设计很大程度上降低了在开发阶段修改数据库结构的麻烦,而且由于表是对应一个个类创建而成,可以很清晰的在任何时候了解到该app的数据库是如何设计的,这对维护的意义也是非常大的。 在这里我们使用常用的User表来做介绍,先上代码。 models.py # -*- coding:utf-8 -*- from sqlalchemy import * from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import scoped_session, sessionmaker #定义数据库的账号、端口、密码、数据库名,使用的连接模块,这里用的是mysqldb engine = create_engine( 'mysql+mysqldb://root:123456@localhost:3306/temp?charset=utf8', echo=False#是否输出数据库操作过程,很方便调试 )

web.py开发web 第四章 Sqlalchemy(事件监听与初始化)

北慕城南 提交于 2019-11-27 02:44:06
上一章讲了sqlalchemy的数据库创建,我想大家应该会慢慢喜欢上用这种方式来创建数据库吧,sqlalchemy不仅仅能自动创建数据库,更提供了其他更强大的功能,今天要介绍的就是sqlalchemy中的事件监听,并将其应用到数据库的初始化中。 上一章的数据库创建,除了将user表创建出来,我们并没有进行其他的操作,而往往我们的web开发,总需要在网站中设计一个超级管理员,这个超级管理员不是注册出来的,而是在数据库创建初期就有的,那么,我们可以在models.py中创建user表,并同时向user表插入超级管理员的信息,而在插入超级管理员或者以后的修改密码、新增用户的时候,我们的密码都是需要加密的,那么这时就可以添加一个事件触发,当插入设置password字段时,自动加密,好,需求有了,接下来就是行动了,我们把原来的models.py修改成以下代码: models.py # -*- coding:utf-8 -*- from sqlalchemy import * from sqlalchemy import event from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import scoped_session, sessionmaker import hashlib

web.py开发web 第五章 视图中使用sqlalchemy

☆樱花仙子☆ 提交于 2019-11-26 16:12:28
前两章讲了一些sqlalchemy的基础使用,这一章要讲的就是在视图中调用sqlalchemy,同时这一章还要讲下web.py的类视图的一个使用技巧。 web.py支持中间件的设置,所以我们先写一个middleware.py来存放中间件。 middleware.py # -*- coding: utf-8 -*- from models import * import web def set_orm(handler): #获取sqlalchemy的session并存储到web.ctx.orm中 web.ctx.orm = bindSQL() #执行视图,如果出现异常回滚数据库,正常结束则提交数据库操作,最终删除session try: return handler() except web.HTTPError: web.ctx.orm.rollback() raise except: web.ctx.orm.rollback() raise finally: web.ctx.orm.commit() web.ctx.orm.remove() 然后在main.py中添加中间件,并定义基类引入sqlalchemy的session,将main.py修改如下。 main.py #-*- coding:utf-8 -*- import web, middleware from web