Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Python program - running it on a website question?

Options
  • 12-11-2013 5:59pm
    #1
    Registered Users Posts: 225 ✭✭


    So basically I am doing Python in college at the moment and have began experimenting with some code at home. I have a pretty useful program that collates statistics from a websites source code based on user input so for me it is useful in the Python shell.
    I want to go one step further, however, and allow people be able to input their details into a form on a website, which would then run my python program with these details and deliver the user the output after computing the stats.

    I am having difficulty finding any concrete way of doing this relatively simply. I would have thought it would simply involve saving the file online and then running it from there but it appears it is more complicated than that.

    Bear in mind the site doesn't need to look great or anything. I would just like one input field and a submit button which when clicked computes and delivers the data, any ideas?

    Greatly appreciate some help as I was a little annoyed it wasn't was easy as uploading the file :(


Comments

  • Registered Users Posts: 60 ✭✭zarafiq


    There is a huge difference for a web server between processing a html file and a python file.
    HTML file is simply read from a disk and sent to the client while your python script must be executed, HTTP request data must be somehow injected into it and it's output must be sent to the client.

    Your web server must know how to execute your python file. Your python script must know how to obtain parameters from a HTTP request (form data submission).
    CGI is one way of connecting python scripts with a web server.

    Google Python and your web server name to find out how to check for/add Python support to your server.
    This book
    http://shop.oreilly.com/product/9780596158118.do
    can be purchased on Android Market (as app) for less than 4 Euro.
    Should answer most of your Python questions.

    As far as I remember Google App Engine supports Python scripts so you can experiment
    with Python and HTML without having to configure your web server.


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    So basically I am doing Python in college at the moment and have began experimenting with some code at home. I have a pretty useful program that collates statistics from a websites source code based on user input so for me it is useful in the Python shell.
    I want to go one step further, however, and allow people be able to input their details into a form on a website, which would then run my python program with these details and deliver the user the output after computing the stats.

    I am having difficulty finding any concrete way of doing this relatively simply. I would have thought it would simply involve saving the file online and then running it from there but it appears it is more complicated than that.

    Bear in mind the site doesn't need to look great or anything. I would just like one input field and a submit button which when clicked computes and delivers the data, any ideas?

    Greatly appreciate some help as I was a little annoyed it wasn't was easy as uploading the file :(

    Might be worth taking a look at https://www.djangoproject.com/

    Disclaimer: I've never written a line of python in my life.


  • Registered Users Posts: 1,606 ✭✭✭djmarkus


    Django is the way to go.


  • Registered Users Posts: 1,922 ✭✭✭fergalr


    I would recommend something like Flask rather than Django.
    http://flask.pocoo.org/

    Flask is designed to be a really lightweight simple web framework.
    Its easy to get started with, and will probably take less learning than Django.


    You'll need a machine to host it on, though. If you already have shell access to a machine on the internet, that'll be easy.


    Otherwise, maybe find a hosting provider that does Flask hosting (maybe https://www.pythonanywhere.com/ - I've never used it.) ; or configure your own EC2 machine (expect to spend maybe a day learning how to work ec2.)


    If you wanted a reliable platform-as-a-service offering, then check out google app engine, as already stated. Expect to spend a few days getting to grips with it, though.


  • Registered Users Posts: 225 ✭✭TheSetMiner


    fergalr wrote: »
    I would recommend something like Flask rather than Django.
    http://flask.pocoo.org/

    Flask is designed to be a really lightweight simple web framework.
    Its easy to get started with, and will probably take less learning than Django.


    You'll need a machine to host it on, though. If you already have shell access to a machine on the internet, that'll be easy.


    Otherwise, maybe find a hosting provider that does Flask hosting (maybe https://www.pythonanywhere.com/ - I've never used it.) ; or configure your own EC2 machine (expect to spend maybe a day learning how to work ec2.)


    If you wanted a reliable platform-as-a-service offering, then check out google app engine, as already stated. Expect to spend a few days getting to grips with it, though.


    Thanks for all replies.

    I have checked out django and google app engine and even downloaded both but after an hour or so I became quite frustrated at how difficult it was to find information on doing the very basics and getting started..

    Looking at flask now, thanks man, it seems easier to follow... Just so I am clear, flask is just a python module right? so I would import it in my python file and add the code given on the flask homepage to the file. Then what would I do? just upload the file to my website?

    I'm checking out pythonanywhere.com at the moment too and it seems quite good so far, will let you know if this solves my problem!

    thanks again


  • Advertisement
  • Registered Users Posts: 1,922 ✭✭✭fergalr


    so I would import it in my python file and add the code given on the flask homepage to the file. Then what would I do? just upload the file to my website?

    Normally, when your web browser accesses a website, there is a program that listens. This program is called a web server. The web server knows how to look for files that live on the same computer as it, in specific places. It knows how to take these files, and 'serve' them to the browser.

    So, for example, it might know how to serve a file '/home/SetMiner/www/homepage.html' when someone comes to the web server looking for 'http://theWebServersIP/users/SetMiner/homepage.html'.


    In other words, it knows how to serve static files to users that ask for them.

    However, you do not want the computer to just GIVE the web browser the static file when they come to the right address. Instead, you want the computer to EXECUTE the file, as if it were a python program, and to somehow give some OUTPUT of that program to the web browser.

    That is a very different task.

    It is not enough to simply upload your python program, as a static file, to a web server.

    You need to have something more special, that knows how to run the python program.

    That's why this is a little more complex than it first seems to you.


    There are many options to solve this more complex program.

    Expect to sink a little bit of time into understanding what is actually going on.

    Flask is one option. If you write your python program in a certain way, using the Flask library, then a special program will know how to EXECUTE your program in response to users visiting a website.

    Supposedly, pythonanywhere makes this easy to do.

    So try and go play with their service, and read their docs.

    This isn't completely trivial to make happen, so expect to spent a little while learning about whatever service you use, if you are complete new to web dev.


  • Registered Users Posts: 225 ✭✭TheSetMiner


    cheers, have done the intro on pythonanywhere.com using flask and it seems like the clearest way of going about this (compared to django and google app engine anyway) so I will probably tinker around with this for the next few days and hopefully get my program running online soon enough! Thanks for the help again, good to know I can come on here for help with this type of stuff.


  • Registered Users Posts: 60 ✭✭zarafiq


    The free sample of the book I mentioned above contains a simple Python CGI script for processing a HTML form.


  • Registered Users Posts: 1,686 ✭✭✭RealistSpy


    So basically I am doing Python in college at the moment and have began experimenting with some code at home. I have a pretty useful program that collates statistics from a websites source code based on user input so for me it is useful in the Python shell.
    I want to go one step further, however, and allow people be able to input their details into a form on a website, which would then run my python program with these details and deliver the user the output after computing the stats.

    I am having difficulty finding any concrete way of doing this relatively simply. I would have thought it would simply involve saving the file online and then running it from there but it appears it is more complicated than that.

    Bear in mind the site doesn't need to look great or anything. I would just like one input field and a submit button which when clicked computes and delivers the data, any ideas?

    Greatly appreciate some help as I was a little annoyed it wasn't was easy as uploading the file :(


    Check out my git, I have some python web app examples. Let us know how you get on.
    https://github.com/PureIso/PythonWebApp


  • Registered Users Posts: 225 ✭✭TheSetMiner


    Decided to bite the bullet and learn javascript for anyone wondering. I hope to get my idea developed by way of a chrome extension instead.
    Thanks for everyones help


  • Advertisement
Advertisement