Live data from Hacker News

I only know PHP. How do I write a Web application in Python?

me.veekun.com

101–110 of 196 posts

Re: I only know PHP. How do I write a Web application in Python?

#101
post #10

I'd say the necessity of this post is why PHP is as popular as it is. With PHP, there are no package managers I have to install; there is no template language to learn; there is often no version control; there is no "framework"; etc. You just drop in a .php file and, most likely, it will just run. If I need a quick dynamic page on my server, it's much easier to do this in PHP than in Python or Ruby. Until Python or R…

There appears to be currently ongoing some sort of shift in web development. At the top end of the scale , people are creating more sophisticated "app like" websites that use sophisticated platforms like AWS and "serious" programming like Scala/Java/Ruby/Python/jQuery etc. Because once you have decided to invest serious money in your web development you want to use the best tech you can and build a very "bespoke" onl…

Regarding your last point... are you talking 'turnkey' as in something like 37 Signal's Basecamp?

Re: I only know PHP. How do I write a Web application in Python?

#102
post #50
post #26

Earlier quoted context omitted.

It's a chicken-and-egg problem. Best I can do is encourage more people to write more Python software, so those crappy free hosts are more inclined to support running it. Until then, I don't think it's terribly unreasonable to pay twenty bucks a month for a server to play with. That's less than AT&T wants to charge me for sending a thousand 160-byte messages.

Don't mess with "crappy free hosts" -- just run it on a free Heroku dyno ( http://www.heroku.com/pricing ). Kenneth ( http://kennethreitz.com ) shows you can get good performance with Flask on the free dyno ( http://flask.pocoo.org/mailinglist/archive/2012/2/22/flask-o... ).

There is also Google App Engine, the free tier is fairly generous as well.

Re: I only know PHP. How do I write a Web application in Python?

#103
post #8
post #3

Say wat you want of php. But it's (insert personal pejorative option) made for it. Sure Its a_little_inconsistent AndSomewhatChaotic But all the basics for the web are incredible accessible right away. With every other language you have to start with a framework. I find php easier than rails to prototype applications and do fast hacks that need a web face. It's simply the right tool for the job on those occasions. Of…

That's a nice sentiment that's completely inaccurate. Even 6 years ago when I first started programming Python, simple Python cgi scripts were very easy to write. It's as simple as: print """\ Content-type: text/html ... """ With %(var)s interpolation, you can even slap in locals() easily to trivially put variables' values into the page.

and you think that manually handling headers is more straightforward than php? I disagree.

Re: I only know PHP. How do I write a Web application in Python?

#104

   The idea is that a transaction starts when a request  starts, and it’s automatically rolled back if there’s an exception. This is behavior you want from the start! It’s half (err, ¼) the point of using a database.
I mostly agreed with the rest of the article but this strikes me as massive blooper. I can see no reason this would be considered good advise.

It's the database equivalent of the GIL, can't figure out what you actually need to lock? Just lock everything!

An example of this going wrong in production, I work in the online gaming (gambling) industry. One of our users won a large jackpot, our code started a transaction, contacted the 3rd party to have them release the funds, recorded the win in the transaction journal, updated the customers balance etc. Then the stored procedure which inserted a row into the outgoing email queue had a small error. This caused all of the database work on our end to rollback.

For what its worth, this was discovered during a reconciliation with the 3rd party and the customer received his winnings.

My point being, this is a horrible way of dealing with database transactions. Only things which are transactional by nature should be carried out within the same transaction. Obviously your code needs to deal with any errors and carry on or report an error as is appropriate.

Re: I only know PHP. How do I write a Web application in Python?

#105
post #101

Earlier quoted context omitted.

There appears to be currently ongoing some sort of shift in web development. At the top end of the scale , people are creating more sophisticated "app like" websites that use sophisticated platforms like AWS and "serious" programming like Scala/Java/Ruby/Python/jQuery etc. Because once you have decided to invest serious money in your web development you want to use the best tech you can and build a very "bespoke" onl…

Regarding your last point... are you talking 'turnkey' as in something like 37 Signal's Basecamp?

To an extent , yes. Although basecamp is something that is intended more as an internal tool that just happens to work over HTTP and is not directed at any specific market niche (other than perhaps general SMB).

Let's think about real estate as an example. I imagine that pretty much every real estate agent will want the following features on their website:

A list of properties (that can be sorted by Price etc)

Search By Postcode/Zipcode.

Then a page for each property with:

Pictures from inside/outside the property.

Asking Price.

Number of bedrooms/bathrooms.

Some blurb about the property.

A form to book a viewing (possibly integrated with calendering at the backend).

Of course they will also want the generic "contact us" pages and pages about how great they are.

You could develop all of this bespoke every time for each estate agent, but then the amount of time you invest and how good you can make it are capped by the amount of money they want to spend on the project. You also have to worry about hoating arrangements for each client.

Alternatively you could just build the best real estate website you can make, host it on your own infrastructure and charge a monthly fee to rent an "instance" of it. Because the website is already built activation can be instant.

As an added benefit (if you get enough users) you now have a huge database of properties which you could potentially use to launch your own property search/comparison website.

You will have customers who will require more custom functionality and will eventually outgrow your "one size fits all" system regardless of how customisable you make it. At this point however they are probably willing to pay a significant amount of money to have a bespoke website developed. Another option at this point would be to license them the sourcecode for your system so that they can take it elsewhere for modification.

Re: I only know PHP. How do I write a Web application in Python?

#107

The idea is that a transaction starts when a request starts, and it’s automatically rolled back if there’s an exception. This is behavior you want from the start! It’s half (err, ¼) the point of using a database. I mostly agreed with the rest of the article but this strikes me as massive blooper. I can see no reason this would be considered good advise. It's the database equivalent of the GIL, can't figure out what y…

The transaction is internal to the database. It's essentially a list of operations to perform, if something goes wrong it provides a list of things to reverse. If the db transaction fails, then there would need to be some code to handle rolling back operations outside of the database. I don't think db transactions were to fault in your situation.

Re: I only know PHP. How do I write a Web application in Python?

#108
Personally, I _like_ PHP because, at the core, it is a collection of utility functions to write webapps quickly. Here's a small set of those function that I would like to see somewhere else (like in Python for example):

Save a file: file_put_contents()

Load a file: file_get_contents()

Trim whitespace: trim()

Print a string (even numbers!): print $str

Dump an array: print_r()

Print the stack trace: print $e->getTraceAsString()

Want to embed HTML? Go ahead, _without_ any of this nonsense: print ""+str(val)+""

Want to check for errors and improve the quality of the code? Go ahead! Use lower level functions like fwrite, fread, etc; most functions return FALSE on error, use try/catch, throw Exceptions, create custom Exception classes, go nuts with over-architecting if that's your thing.

Also, as someone who knows C and C++ rather well, I find PHP completely intuitive, very easy and flexible, as opposed to:

  if __main__=="__main__": 
      main()
and passing self around is just plain redundant.

The icing on the cake is that a huge part of the built-in functions wrap and extend existing C functions: strpos, strlen, so the language rarely gets in your way. The docs are pretty good (there's room for improvement).

So you can be a zealot all you want, but the truth is: the language doesn't make your code reliable - YOU make the code reliable and, from my experience, people who bash a language rarely know what they're talking about and it usually just boils down to personal preferences like braces vs no-braces.

Lastly, I wish you all the best in your fanatic bashing of a good tool that has proven itself appropriate for web applications. Everyone's tired of the following example, but I'm going to say it anyway: Facebook (and thousands of other large websites). Feel free to be in denial about that.

Re: I only know PHP. How do I write a Web application in Python?

#109

Is there any cheap & easy webhosting available for Python? Occasionally I get commisioned to build small websites ( I don't want the hassle/expense of having to setup and run VPS/AWS infastructure. I just want to give someone a bunch of files and say "sign up for some cheap webhosting, ask them to give you a DB called X and upload these". I usually end up just doing it in PHP, but it has always felt like a poor reaso…

Heroku is a good choice,

> This quickstart will get you going with Python and the Flask web framework on the Cedar stack. For Django apps, please see the Django quickstart.

https://devcenter.heroku.com/articles/python

It can be very cheap or free at low scale,

> Each application receives 750 free dyno hours per month.

https://devcenter.heroku.com/articles/dynos

Re: I only know PHP. How do I write a Web application in Python?

#110

Is there any cheap & easy webhosting available for Python? Occasionally I get commisioned to build small websites ( I don't want the hassle/expense of having to setup and run VPS/AWS infastructure. I just want to give someone a bunch of files and say "sign up for some cheap webhosting, ask them to give you a DB called X and upload these". I usually end up just doing it in PHP, but it has always felt like a poor reaso…

You can get close to that with Webfaction (some assembly required) or Heroku.
Post reply on HN