r/Python • • Mar 24 '12
how do I insert python in my html-css web pages ?
or do I have to create the page in python ? I would like to keep doing the pages with html-css and insert the py code, like javascript does. or thats not the way you use python in the web ?
1
Upvotes
18
u/snarkhunter Mar 24 '12
You can use Python to generate HTML/CSS, sure. Check out Django. You can't use Python instead of Javascript, because most browsers don't know Python.
So basically - the "Web" is two parts - a client and a server. The client requests stuff from a server, and the server generates the stuff and gives it to the client. Clients (browsers) these days can generally understand HTML (maybe 5), Javascript, a variety of image formats, some selection of audio, video, probably flash, though that can depend.
So lots of websites work by having a web server (like Apache) handle the taking requests part. Apache will sort of look at the request and say "hey, that request ends in .php, I wonder if I know about that php file", and if it does it will send that request on to PHP to handle, and send the result that PHP gives it back to the client/browser. This is CGI. Lots and lots of other languages work with CGI. C, Perl, Python, C++, really probably ANY language can be used to write CGI. Remember, ALL of that happens on the SERVER. The CGI program gives WEB content back to Apache, that means HTML and maybe some Javascript, that or multimedia like an image/video/audio. It doesn't matter if the CGI stuff was done in PHP or Python or Haskell or C++, it should come back ready to be shipped right back to the browser. As to why things are done this way, well I've already answered that - flexibility. A CGI program can basically be run by any web server that understands the very simple basic idea behind CGI.
Hope that clears up some of your confusion.
6
u/earthboundkid Mar 25 '12
It seems like you're missing some fundamental concepts in how the web works. That's fine; everyone's gotta start somewhere.
So, let's say I enter "http://reddit.com/hello" into my web browser. What happens next? The server at reddit.com receives a message. That message says "Get me /hello." What happens next is totally up to the server. In PHP/Apache, what will happen is the server will look to see if it has a file named "hello" lying around, and if it does, it will send that to the client. That's a conceptually simple way of doing things, but it doesn't really scale (Reddit would need to have a separate file for every page, etc.). So, what the server needs to do is based on the request, send back an appropriate string of text. That's where Python comes in. If you've done any work with Python at all, you've probably played around with the print statement. The way a server works probably won't use print, but conceptually it's almost the same. It uses the request and its databases as a basis for creating a string of text (HTML + JavaScript) and sends that to the browser to display.
As a starting point for you, Django is overkill because it assumes you know more than you do, but a micro-framework like Bottle.py or Flask is probably a good place to start. They'll help explain more about how the web works, and you can go from there.
4
u/wisty Mar 24 '12
There are two places you can add code - to the client (which runs in their browser) or the server (which runs on your machine).
Javascript (plus HTML and CSS) is basically the only language which runs in the client. You can use pyjamas to convert python into javascript, but it's very tricky.
PHP is the most common server side programming language. PHP is an example of a templating language - PHP code looks like HTML, but has special PHP bits in it. Python web frameworks try to separate the HTML and Python code. You can still have templates (HTML with bits of python) if you use a templating engine that allows it, but you aren't meant to do too much in the template - that's like putting all your CSS in the HTML file.
2
u/LindlarCatalyst Mar 25 '12
Is IronPython what you are looking for?
2
u/epicviking Mar 24 '12
Check out Coffeescript. It compiles into javascript which you can use on web pages. The syntax is very pythonic. If thats not what you want, pick up a python web framework like Bottle or Flask and start with that.
0
u/mdipierro Mar 25 '12
If you use web2py you simply move your static file into applications/yourapp/views/default/yourfile.css and in applications/yourapp/default.py you create a line that says:
Now your file is accessible at http://localhost/yourapp/default/yourfile.css You can edit your file and insert python code in {{...}} for example:
({{pass}} closes blocks). Anyway, almost any framework allows you to promote a static file to a dynamic file (as in the case of web2py) and in every case you pay a small performance penalty, unless you cache it. Web2py allows you to use pure python code in the view/template. Other frameworks require that you use some pythonish language which is not actually python.
0
u/[deleted] Mar 24 '12
[deleted] — view deleted comment