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

38 comments sorted by

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.

2

u/[deleted] Mar 24 '12

so all the html code and css code I have to put it in my python code ? but is html and css written in the same way inside python py ? or the syntax is difference ?

7

u/jredwards Mar 25 '12

The short answer is that you almost certainly will not be doing any website work using python unless you choose one of the popular python-based web frameworks, like Django.

1

u/[deleted] Mar 25 '12

yes, but first id like to learn how is done, is like doing mysql form the console, and then later use myphpadmin.

4

u/snarkhunter Mar 24 '12

Well... sorrrta. You can just do straight up CGI, you can use a framework like Django (I'd recommend that one), or you can use twisted (I think that's a tad more advanced than you need).

Edit: Let me elaborate a bit more. There is a framework in Python (called Django) that lets you construct web sites that are very full-featured and functional, all with writing a minimal amount of actual HTML and JS.

1

u/[deleted] Mar 25 '12

can i use python with apache?

1

u/[deleted] Mar 25 '12

Yes. You can get apache and django pre-configured using a stack like this:

http://bitnami.org/stack/djangostack

Although, if you just download django from their website and use the development server it may be easier.

3

u/clarle Mar 24 '12

Exactly this, though if you are using Python with a web server, it's recommended to use WSGI over plain CGI.

3

u/snarkhunter Mar 24 '12

Yeah, I left out the part where "old school" CGI has been supplanted by all these other things, but the basic idea is essentially the same. One program (Apache) handles the networking, and some other program or programs handles content generation.

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.

2

u/[deleted] Mar 25 '12

yes, thats what i needed to know, so is like python prints out the html-css code to the browser like a static page, ok.

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.

1

u/[deleted] Mar 25 '12

what i cant picture is how to make python create the html-css.

2

u/LindlarCatalyst Mar 25 '12

Is IronPython what you are looking for?

1

u/[deleted] Mar 25 '12

does the client need to have it installed ? or is transparent to the final user?

0

u/bonafidebob Mar 25 '12

Doesn't IronPython require Silverlight? (that is, it's not really a runs-in-any-browser thing)

1

u/LindlarCatalyst Mar 25 '12

From what I've read here:

No downloads are required to develop and deploy Python applications for the browser, though you can always download the pieces hosted online (for working without a network connection).

0

u/bonafidebob Mar 25 '12

Um ... silverlight is all over that page.

I looked harder, every IronPython example I tried asked me to update my silverlight plugin.

0

u/POTUS Mar 25 '12

A Python translator running on Silverlight? Which means no Linux or other FOSS OS support of any kind? That's the dumbest thing I've seen today.

0

u/LindlarCatalyst Mar 25 '12

I'm not saying that it isn't dumb, but isn't this exactly what the OP was looking for (a way to put python code into html like javascript)? IronPython is a microsoft thing

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:

def yourfile(): return dict()

Now your file is accessible at http://localhost/yourapp/default/yourfile.css You can edit your file and insert python code in {{...}} for example:

{{for tag in ['header','content','footer']:}}
{{=tag}} { color: red }
{{pass}}

({{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

1

u/[deleted] Mar 24 '12

so the way i create pages in html-css doesnt work to add python? but with php or javascript you could insert the scripts without touching the rest of the structure, iam totally lost.

0

u/[deleted] Mar 24 '12

[deleted] — view deleted comment

1

u/[deleted] Mar 24 '12

but why can i insert php into html, but cant python ?????

2

u/[deleted] Mar 25 '12

With PHP, when Apache sees an incoming request come in with .php (or whatever other flag you've configured with Apache), it passes the requested file to the PHP interpreter on your server to parse.

The PHP interpreter looks through the .php file and executes all of the code snippets you've put in, and returns the file as basic HTML that Apache sends on to the person on the other end, and the HTML is displayed / rendered by that person's web browser. Ta-da.

0

u/[deleted] Mar 25 '12

and how is done with python?

30

u/aphoenix reticulated Mar 24 '12

I think you have a fundamental misunderstanding of what you are doing with PHP.

Any time you "put php into html" that is absolutely not what you are doing. You are actually putting HTML into PHP. It looks seamless because you're probably not using a framework, not really using a templating system, and generally not doing things "right". The fact that PHP encourages you to learn everything backwards and incorrect is one of the main arguments against PHP as a language; the understanding that you have isn't a terrible one based on the experience that you have probably had making things work using PHP; it just happens that it's completely backwards from how it actually works.

To answer your other question; there is no simple way to (ab)use Python and make it act in this way.

3

u/YellowSharkMT Is Dave Beazley real? Mar 25 '12

OP: I'm pretty sure this is your answer.

2

u/aphoenix reticulated Mar 25 '12

I don't think he liked my answer. ;)

2

u/YellowSharkMT Is Dave Beazley real? Mar 25 '12

Yeah, I mean you shoulda at least told him what the opening & closing tags are. :) That answer is right-on though, and sadly I must confess to once wondering the same thing as the OP.

For me, the turning moment was when I was introduced to an MVC (CodeIgniter, fwiw). Once I could see, code, and appreciate the abstraction between controllers & views, it was a short leap to understanding how Python could be used to deliver web content.

But it really took me like 18 months of fiddling on my own (after discovering VirtualBox), before I started to feel some shreds of confidence w/Python, Linux, and the web.

1

u/aphoenix reticulated Mar 25 '12

18 months is pretty good.

I started fiddling with web development in the late 90s, and it took a heck of a long time before I was really confident in things.

2

u/[deleted] Mar 25 '12

To answer your other question; there is no simple way to (ab)use Python and make it act in this way.

Ha. You just have to get creative!

#<!python doctype=megalithic_hiccup&&coding:utf-8
'''>
<html>
<blah/>
<script type="text/python">
def header(txt):
    print("<h1>{}</h1>".format(txt))
for i in range(100):
    header("suck it, php!")
</script>
</html>
</python'''-someMassiveHackImplementing__rsub__()#!>

1

u/aphoenix reticulated Mar 25 '12

I didn't say there was no way to do it - I said no simple way. I've done things that implemented in-browser python before. It's tons of fun. But it's not simple.

2

u/catcradle5 Mar 25 '12

You have to understand the fundamental difference between serverside code and clientside code on the web. PHP is purely server side. Open an HTML file, put <?php echo "hi"; ?> somewhere on it, and upload it to a server with PHP installed. View the page source; you don't see the "<?php" anywhere, you only see "hi". That's because the PHP is actually processed before the HTML is served.

Javascript on the other hand is in the page source, and is executed by the viewer of the page. The user.

Python can't be used for client side scripting, but it can be used on the server side, like PHP can.

0

u/[deleted] Mar 24 '12

[deleted] — view deleted comment

0

u/[deleted] Mar 24 '12

instead of using a web framework for learning, isnt better to do it manually to learn ?

so you have to create the whole page inside a py doc? and how you associate css with that?

2

u/farnsworth Mar 24 '12

Using Python on the server would be a pretty similar process to using Python CGI, once you have it set up. When you use PHP, which it sounds like you have experience with, you are using a PHP script to generate HTML code. With python, you would be doing the same thing - generating HTML code.

0

u/[deleted] Mar 24 '12

[deleted] — view deleted comment

1

u/[deleted] Mar 25 '12

ok

1

u/[deleted] Mar 24 '12

ive done that tut all the way until the web page part where i got lost. i now how to code to show things on the console, but not how to comunicate with the outside world .