Live data from Hacker News

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

me.veekun.com

181–190 of 196 posts

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

#181
post #162

Earlier quoted context omitted.

I get the feeling you aren't very familiar with Python. Actually I'm hard-pressed to name a popular language in which writing a string to a file is particularly difficult. You don't have to check for errors in Python; most functions raise exceptions on error, rather than letting your program continue with a known bad state. The `if __name__ == "__main__":` pattern is a cute trick to distinguish a module from a file b…

> a cute trick allow me to disagree about the cuteness of that. there are ways to distinguish a module from an executable file. For example, 'main' could be a special function (like in C). > you just list it as a parameter yes, that's what I wanted to say. the fact is, in other languages I don't have to list it as a parameter and I don't have to get used to it. Personally, I find it annoying (but I could live with it…

Strictly speaking, `main` isn't even a special function in C. It's only special to your C library and linker and something something I forget the details. Anyway it doesn't matter at all; you could leave off the `if` in Flask's example and never notice any difference. Just emerged as common practice to bracket off stuff like that /in case/ you ever decide to import your script as a module.

Part of my problem with PHP is that it doesn't seem to know what it is; there are the C parts, and the C++/Java parts, and both sides are sort of glued together with Perl influence, yet don't very well interoperate otherwise.

I have a pretty patchwork background and I could get work done in just about any language you'd pay me to use. I'd even happily write a web app in C. Doesn't make PHP any more attractive.

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

#182

Earlier quoted context omitted.

I agree on the simplicity of php. The barrier to write php is low. All you need is notepad and browser. This may be a transition from writing html. Compare this to other languages where you need multiple tools before you can even begin. This doesn't make php better. It just may explain why less people make the jump.

You also need a server, which I think is one of the crucial components for PHP adoption. MAMP, XAMPP, WAMP are all really easy to install and you don't need to go into the CL to do anything.

[deleted]

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

#183
post #168
post #98

By far the easiest way to get started with Python web development is Web2py which comes as a self-contained install where you can get started in 5 minutes. So you automatically get "request" information, big deal.

Flask comes as a self-contained install where you can get started in 5 minutes.

Flask doesn't help much with the database.

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

#184
post #16

Earlier quoted context omitted.

Most of the post _isn't_ strictly necessary. You don't need a database to get your feet wet. You don't need to care about XSS for your first throwaway app (though, arguably, you should). I pimp Mako precisely because there's no separate language to learn: the core syntax is just Python. I tried to describe the _ecosystem_, and I assume any serious PHP application will care about most of the same things. Here's all yo…

I am lucky that I found Flask a while ago, because I was sick of PHP. Flask, jinja2, and uWSGI have since been my best friends. It took a little while to figure out how to setup a proper web server around those, but the guides are not that complex. Install nginx, darmonize uWSGI with upstart, and configure it. It's funny, in some ways it's easier to use than PHP, because when I am working with the website on my local…

In fact, I felt impassioned about it enough to create a blog and write about it:

http://kramerapps.com/blog/post/22551999777/flask-uwsgi-ngin...

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

#185
post #146
post #134

Earlier quoted context omitted.

Not sure about the Python ones, but the Ruby read/write file examples aren't quite fully done/correct here: # Equivalent of file_put_contents() in PHP # This could also be used for appending if the correct mode is specified. File.open("filename", "w") { |f| f.write("some data") } # Equivalent of file_get_contents() in PHP some_var = File.read("filename")

Ruby 1.9.3 added File.write("filename", "some data").

Cool. Didn't know that. Thanks.

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

#186
post #181

Earlier quoted context omitted.

> a cute trick allow me to disagree about the cuteness of that. there are ways to distinguish a module from an executable file. For example, 'main' could be a special function (like in C). > you just list it as a parameter yes, that's what I wanted to say. the fact is, in other languages I don't have to list it as a parameter and I don't have to get used to it. Personally, I find it annoying (but I could live with it…

Strictly speaking, `main` isn't even a special function in C. It's only special to your C library and linker and something something I forget the details. Anyway it doesn't matter at all; you could leave off the `if` in Flask's example and never notice any difference. Just emerged as common practice to bracket off stuff like that /in case/ you ever decide to import your script as a module. Part of my problem with PHP…

In implementation terms the main function isn't special, but its prototype is part of the C specification: http://groups.google.com/group/comp.std.c++/browse_thread/th...

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

#187
post #137
post #94

Earlier quoted context omitted.

See the Heroku Quickstart: https://devcenter.heroku.com/articles/quickstart Essentially you sign up for a Heroku account ( https://api.heroku.com/signup ), and set up the Heroku "toolbelt" ( https://toolbelt.heroku.com ). Once that's done, you can run this bash script ( https://gist.github.com/2622850 ) to create and deploy a "hello world" Flask app on Heroku: $ heroku login $ bash setup.sh helloworld $ cd helloworld…

Compared to deploying Hello World in PHP, that's a lot of work.

And compared to putting down the window in my car, turning on the AC is a lot of work. Two buttons and a dial compared to just one button.

One is clearly superior to the other though and anyone going near a car should have zero difficulty with either...

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

#188
post #143

Earlier quoted context omitted.

Except that Java strings aren't in Unicode. They're in UTF-16, which is the worst-of-all-worlds encoding. (It's big and heavy, and it still has multibyte sequences. They're just rare enough that you're likely to forget about them during testing.)

UTF-16 is most certainly Unicode, even if it isn't the preferred flavor.

In Java, you end up seeing the guts of UTF-16 far more often than you should. Most notably, the String APIs often index strings by UTF-16 code units, not characters, so string "lengths" don't always correspond to the number of Unicode characters, and you can end up cutting surrogate pairs in half if you aren't careful.

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

#189
post #103
post #8

Earlier quoted context omitted.

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.

"Manually handling headers"? That is how people wrote websites in the early 2000s. Many cgi scripts were cobbled together in Perl. I found that I liked Python better.

I understand that in PHP you just add some stuff to an otherwise normal HTML file, but the situation in Python, with its multiline strings that support variable interpolation, isn't much worse. Just add the lines I mentioned to the top (treat is as a recipe if you like), and the rest is better than PHP.

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

#190
post #52

Earlier quoted context omitted.

CGI scripts have much of PHPs simplicity. You can just drop in a executable file, and depending bit on server configuration, it will just run. With Python a basic hello world would look something like this: #!/usr/bin/python print "Content-type: text/plain" print "" print "Hello World!" So 3 lines of "boilerplate", and then you can do anything you want. No extra packages to install or languages to learn. No framework…

Let's look at the PHP version: Hello World! One line. I didn't have to worry about Linux/Windows, knowing HTTP headers or the fact that I need two CRLFs after the last header[1]. I just wanted a page that says "Hello World!" and that's what I got. Don't get me wrong, I would never choose to write a web app in PHP over python. But it is brain dead simple to go from nothing to something in a few keystrokes. Which is pr…

Yeah, PHP is great to start off with, it's simple and everything in the language is obvio- Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
Post reply on HN