I have a Python program that takes one input (string) and prints out several lines as an output. I was hoping to get it online, so that the web page only has a text-area and a button. Inserting a string and pressing the button should send the string to python and make the original page print out the output from Python.
Assuming I have only dealt with Python and a little bit of HTML and have very limited knowledge about the web, how might I approach this problem?
For some reason, my first instinct was PHP, but Googling "PHP Python application" didn't really help.
That is, assuming what I want IS a 'web application', right? Not a web server, web page or something else?
How could I get a really simple version (like in the first paragraph) up and running, and where to learn more?
Yes, the term you're looking for is "web application". Specifically, you want to host a Python web application.
Exactly what this means, can vary. There are two main types of application - cgi and wsgi (or stand-alone, which is what most people talk about when they say "web app").
Almost nobody uses cgi nowadays, you're much more likely to come across a site using a web application framework, like Django or Flask.
If you're worried about easily making your page accessible to other people, a good option would be to use something like the free version of Heroku do to something simple like this. They have a guide available here:
https://devcenter.heroku.com/articles/getting-started-with-python#introduction
If you're more technically inclined and you're aware of the dangers of opening up your own machine to the world, you could make a simple Flask application that does this and just run it locally:
from flask import Flask, request
app = Flask(__name__)
#app.route('/')
def main():
return 'You could import render_template at the top, and use that here instead'
#app.route('/do_stuff', methods=['POST'])
def do_stuff():
return 'You posted' + request.form.get('your string')
app.run('127.0.0.1', port=5555, debug=True)
Pick a Python web development framework and follow the getting started tutorial. Main options are Django for a "batteries included" approach or Flask for a minimalistic bottom-up approach.
Related
I am building a web application that contains calculators for various industries. I am using html and css for the layout(color, size, etc). However, the core funtionality like arithmetic operations will be done using pure python programming and not javascript. So I will have html, css, and python files. Question is - how can I link my python file to html?
You can't use python to run in the browser on the client's machine in the same way as JS, just because browsers are built to execute JS and not Python. However if all you want to do in the python is backend work, that is definitely possible.
I would suggest looking at Flask a popular python web framework that is quite easy to get started with.
I could think of two easy ways right away, either stick to a solid framework, like django. Or you can make python scripts that manipulate .js files since text manipulation is easy and fun in python. But there are many more possibilities, like Flask and more.
You can use Brython
Brython is designed to replace Javascript as the scripting language for the Web. As such, it is a Python 3 implementation (you can take it for a test drive through a web console), adapted to the HTML5 environment, that is to say with an interface to the DOM objects and events.
If the only function you want from the python(and the server side in general) is the calculation, I would suggest taking a serverless structure. It's simpler, less low-level work, easier to upgrade and scalable.
An example of build one with python is with Zappa (AWS lambda)
I know I'm two months late, but if you're still set to using Python with your HTML/CSS website, it is completely doable.
However, as the previous answer mentioned, if it is only for calculations and light functionality, you're better off using javascript.
Please refer to this link to get an idea on how to integrate python using Flask with your HTML website.
I would recommend you to use flask as a backend as big platforms such as youtube use Django(which is a python framework). And I am sure that js is powerful that it can handle arithmetic operations you can check the webs out for that. But if you want to use flask it is very easy here is the basic structure
from flask import Flask, render_template
import pyautogui
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
app.run()
Keep in mind that python is limited to things and it would be hard to publish a website with python as backend
I have an idea for a web app and plan to learn Python as I go (right now I know html/css, some javascript, some php and sql). The app would be able to manipulate and analyze audio files, among other things.
Ideally, I'd like to make the app available through my wordpress site so that I can take advantage of WordPress's login management and the plugin s2member's subscription management and content restriction capabilities.
Is that possible? Would it even make sense?
If not, is there a better alternative to automate all of that (the subscription management, logins, payment processing, content restriction, etc) without having to code it myself?
I suggest you develop a REST API in Python and extend your Wordpress site to consume that API.
For the Python side, you could go with Flask and use Flask-RESTful.
For the Wordpress side, have a look at this question.
Sure, if you meet a couple of conditions:
The server your wordpress site is on also has python
And you have the ability run arbitrary python scripts on said server.
Here's a (very contrived) example of how to do it from a plugin:
call-python.php (plugin file):
<php
/*
Plugin name: Call Python
Author:..
....
*/
$pyScript = "/path/to/app.py";
exec("/usr/bin/python $pyScript", $output);
var_dump($output);
And the python script app.py:
print("Hello, World")
And that's it! That will dump Hello, world to the body. Obviously you'll need a bit more for a more complicated python app, but it will work.
Like others are saying, there may be better "more correct" ways of doing it. But if your end goal is to run a python app from WordPress it's possible.
folks.
I'm completely new to Pyhton development and i want to know some basic things.
I need to make a bot that access an API and want it to keep running on a server. I did some search and found Google App Engine + python as the best solution for this. So, i have a code example but its a "desktop" app.
I want it to be running on a server (Google App Engine or something like it), every X minutes and when i access the web adress of it, i would like to see the log on screen (just for monitoring).
Here's the code example (of course i don't need the input things and so. It would be hard coded variables in the application. And i know the "print" method wont work as well):
GitHub Code
Coud you guys help me?
Start here for App Engine intro: https://cloud.google.com/appengine/docs/python/gettingstartedpython27/introduction
Then see GAE Cron docs here to "do something every X minutes": https://cloud.google.com/appengine/docs/python/config/cron
If you have any specific questions / problems once you start porting your app - come back and post them. I glanced over your code quickly and don't think you should run into any issues although don't expect anyone here to do your job for you.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
How to create simple web site with Python?
I mean really simple, f.ex, you see text "Hello World", and there are button "submit", which onClick will show AJAX box "submit successful".
I want to start develop some stuff with Python, and I don't know where to start.
I was hoping more elaborate answers would be given to this question, since it's a sensitive subject. Python web developing is split across lots of frameworks, each with its ups and downs and every developer using a different one. This is quite unfortunate. What you should know:
Use WSGI. Don't use anything else, WSGI is the latest standard in Python web developing;
Don't develop directly on top of WSGI, unless you really have to (not even for a hello world app);
Use a framework that best suits your needs:
I played with Werkzeug (which is not really a platform, they call it a toolkit) because it's really simple yet powerful. It lets you work on the WSGI level (also helps you understand how WSGI works) while providing really useful features and helpers. On the Werkzeug website you will also find useful tutorials and things like that.
Probably the most popular framework is Django. Never used it, but maybe there's a reason for why it is so popular.
In conclusion, use whatever is closest to your heart.
You can write a web site with Python in which the web server is implemented in Python, or in which Python is called from some other web server. If you do not already have a web server set up, the first option is easier. The Python library includes a fully functional web server, all you have to is add a couple of methods to respond to requests.
For a complete example of a web site using this simple technique, see Making a simple web server in Python
This technique may or may not serve you well for developing commercial, production web sites, but it's the simplest way from P(ython) to W(ebsite).
Why don't you try out the Google AppEngine stuff? They give you a local environment (that runs on your local system) for developing the application. They have nice, easy intro material for getting the site up and running - your "hello, world" example will be trivial to implement.
From there on, you can either go with some other framework (using what you have learnt, as the vanilla AppEngine stuff is pretty standard for simple python web frameworks) or carry on with the other stuff Google provides (like hosting your app for you...)
As Felix suggested, definitely use WSGI (mod_wsgi) as your gateway interface. It is the modern way of doing business and the other major contendor, mod_python, is no longer being maintained.
Django is a great choice if you want a full-fledged production-quality framework but it also comes at the cost of having a lot of overhead and a pretty steep learning curve.
My suggestion is: Tornado!
I have found that Tornado makes it very easy to get up and running quickly. To illustrate here is the "Hello, World" from the Tornado documentation:
import tornado.httpserver
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
In my opinion that speaks for itself.
Edit: It's important to note that you don't have to use the web server that comes with Tornado. It plugs very easily into WSGI to run with whatever server you already have going.
Best of luck in your search!
You can take this course offered FREE on udacity Web Development using Python.
This is great course and teaches from scratch using GAE. At the end of the course you would have a full fledged blog of yours on web developed by you in python.
P.S one of the instructor is Steve Huffman founder of Reddit.
I think you should start with some kind of Python web framework. For me Web2Py is both easy and powerful. Of course you can create your pages using CGI: no framework required, but for more complicated sites it is not practical.
I am looking for a LAMPish/WAMPish experience.
Something very transparent. Write the script, hit F5 and see the results. Very little, if any abstraction.
SQLAlchemy and (maybe) some simple templating engine will be used.
I need simple access to the environment - similar to the PHP way. Something like the COOKIE, SESSION, POST, GET objects.
I don't want to write a middleware layer just to get some web serving up and running. And I do not want to deal with specifics of CGI.
This is not meant for a very complex project and it is for beginning programmers and/or beginning Python programmers.
An MVC framework is not out of the question. ASP.NET MVC is nicely done IMO. One thing I liked is that POSTed data is automatically cast to data model objects if so desired.
Can you help me out here?
Thanks!
PS: I did not really find anything matching these criteria in older posts.
CherryPy might be what you need. It transparently maps URLs onto Python functions, and handles all the cookie and session stuff (and of course the POST / GET parameters for you).
It's not a full-stack solution like Django or Rails. On the other hand, that means that it doesn't lump you with a template engine or ORM you don't like; you're free to use whatever you like.
It includes a WSGI compliant web server, so you don't even need Apache.
For low barrier to entry, web.py is very very light and simple.
Features:
easy (dev) deploy... copy web.py folder into your app directory, then start the server
regex-based url mapping
very simple class mappings
built-in server (most frameworks have this of course)
very thin (as measured by lines of code, at least) layer over python application code.
Here is its hello world:
import web
urls = (
'/(.*)', 'hello'
)
app = web.application(urls, globals())
class hello:
def GET(self, name):
if not name:
name = 'world'
return 'Hello, ' + name + '!'
if __name__ == "__main__":
app.run()
As much as I like Werkzeug conceptually, writing wsgi plumbing in the Hello, World! is deeply unpleasant, and totally gets in the way of actually demoing an app.
That said, web.py isn't perfect, and for big jobs, it's probably not the right tool, since:
routes style systems are (imho) better than pure regex ones
integrating web.py with other middlewares might be adventurous
What you're describing most resembles Pylons, it seems to me. However, the number of web frameworks in/for Python is huge -- see this page for an attempt to list and VERY briefly characterize each and every one of them!-)
Have you looked into the Django web framework? Its an MVC framework written in python, and is relatively simple to set up and get started. You can run it with nothing but python, as it can use SQLite and its own development server, or you can set it up to use MySQL and Apache if you'd like.
Pylons is another framework that supports SQLAlchemy for models. I've never used it but it seems promising.
Look at:
WSGI, the standard Python API for HTTP servers to call Python code.
Django, a popular, feature-rich, well documented Python web framework
web.py, a minimal Python web framework
Don't forget Bottle. It is a single-file micro web framework with no dependencies and very easy to use. Here is an "Hello world" example:
from bottle import route, run
#route('/')
def index():
return 'Hello World!'
run(host='localhost', port=8080)
And here an example for accessing POST variables (cookies and GET vars are similar)
from bottle import route, request
#route('/submit', method='POST')
def submit():
name = request.POST.get('name', 'World')
return 'Hello %s!' % name
Check out web2py. It runs out of the box with no configuration - even from a USB stick. The template language is pure Python and you can develop your entire app through the browser editor (although I find vim faster ;)