Python AppEngine And HTML - python

I have a simple Hello World application in Python that i'm using with AppEngine, but i want to insert this in a HTML file, like this: I have a file called test.html and on it i have this snippet:
<center><img src="test.png></center>
// Here comes the Python App //
I want to put the output of the Python application in this space, like i can do with Servlets(Java).
Regards.

You should work through the getting started docs at the AppEngine site. Specifically around templates:
http://code.google.com/appengine/docs/python/gettingstarted/templates.html.

Use the Django templates - the module comes built in with the app engine. I've used it here on line 42. The template used is here.

I would really recommend using a boilerplate template
https://github.com/coto/gae-boilerplate
This would give you the layout and template structure for developing useful and professional sites

Related

Import python projects to a HTML page

Supose I have a python game and I want to "post" it on a site like Friv that I am making. Is there any way
for me import the "game.py" to the "site.html" and it show when I enter the site? I made a search and found to use django, but I would need to pass all the html code that I already have to other aplication.
The language of browsers is JavaScript.
There is a project called PyJs which translates Python code to JavaScript and is useful in your case that you want to run Python code inside web browsers.
Finally you can use your resulting JavaScript files to fill up your HTML page.
In addition to PyJs, there are numerous other projects that "run Python code in a browser" like Brython. However, any of them have not been standardized and if you want a robust game in your browser, use JavaScript!
There are number of projects that compile python into JavaScript in order to be run on browser.
Here are two links that might help
Web Browser Programming: https://wiki.python.org/moin/WebBrowserProgramming
PyGame Trinket: https://trinket.io/features/pygame
The way I integrate python code in an html is to use templating language like jinja2 but if you want to write full python code in html then use need to use a transpiler like PyJS but since you want to integrate the same code in multiple program, why not use FLASK it is much more easier.
and make an api. Django is an option but it has a steep learning curve. you can make the UI using HTML and get the data from python using API.

Need to run separate python apps using web.py, examples on site don't work

Trying to build small python apps to have them hosted and accessible from a main web page, stored on server in their own subdirectories. I am on python2.7 with web.py 0.37
On http://webpy.org/multiple_apps the key piece of code is simple:
"""run.py""" import blog
import wiki
import delegate
mapping = (
("/blog", blog.urls, blog),
("/wiki", wiki.urls, wiki)
)
if __name__ == "__main__":
delegate.run(mapping)
AND the first offending bit of code is in the the last line of delegate.py:
web.run(handler, {})
The code for delegate.py is part of the example linked in the first paragraph. The 'run()' function is in web.application(), not web, so that's the first error. I just don't know how to get around it. Perhaps I am trying to use web.py the wrong way?
I have looked through documentation, some other examples and the google code forum with no luck. Also I've tried replacing the mapping tuple with the urls = (url, class) set up, no success.
I'm new to python and web.py, what am I missing?
The multiple apps example code at webpy.org is obsolete. Fortunately, it's even easier to implement now.
See the answer Django style multiple apps with web.py not working

Python web framework

Is there a Python web framework where the Python code and the HTML code are in the same file?
Django uses templates with a special language to generate HTML. Pylons uses a kind of language whose name is Mako.
I prefer to output HTML using calls like:
echo('''
HTML string
''')
or
print '''
HTML string
'''
instead of special languages.
Edit:
Can I do something like PHP?
echo( '''<p>Something</p>''' )
if cond:
echo( '''<p>Another thing 1</p>''' )
else:
echo( '''<p>Another thing 2</p>''' )
If you want to use print itself you may want to rely on cgi.
Going for templates, the closest you can get is from webpy project's Templator
The web.py template language, called
Templetor is designed to bring the
power of Python to templates. Instead
of inventing new syntax for templates,
it re-uses python syntax. If you know
Python programming language, you will
be at home.
This might be what you prefer, but it's really against every best practice ever. That being said, you can do this with django.
from django.http import HttpResponse
dev some_view(request):
return HttpResponse("This is foolish short term gain")
The web2py template engine allows you to use pure Python in your HTML templates (see details in the book). You can also use web2py's HTML helpers to build HTML in Python (this generates a server-side DOM that can be queried and manipulated before rendering to HTML). If desired, the template engine can be used independent of the framework (the relevant module is /gluon/template.py). If there's something in particular you want to do, feel free to ask on the mailing list.
Divmod Nevow will allow you do do that with either prints or a custom Python expression-based language that allows you to embed XHTML code directly into your scripts. However, it's beta-quality, based on Twisted and thus a bit unconventional, and the main site for the company that develops it is down, so I can't point you at a tutorial or anything right now. Use at your own peril.
The special syntax (called stan) syntax looks more or less like this:
tags.html[
tags.head[ tags.title[ "Greetings!" ]],
tags.body[
tags.h1(style="font-size: large")[ "Now I will greet you:" ],
tags.span[ "Hello, world" ]
]
]

How do I create a web interface to a simple python script?

I am learning python. I have created some scripts that I use to parse various websites that I run daily (as their stats are updated), and look at the output in the Python interpreter. I would like to create a website to display the results. What I want to do is run my script when I go to the site, and display a sortable table of the results.
I have looked at Django and am part way through the tutorial, but it seems like an awful lot of overhead for what should be a simple problem. I know that I could just write a Python script to output simple HTML, but is that really the best way? I would like to be able to sort the table by various columns.
I have years of programming experience (C, Java, etc.), but have very little web development experience.
Thanks in advance.
Have you considered Flask? Like Tornado, it is both a "micro-framework" and a simple web server, so it has everything you need right out of the box. http://flask.pocoo.org/
This example (right off the homepage) pretty much sums up how simple the code can be:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
If you are creating non-interactive pages, you can easily setup any modern web server to execute your python script as a CGI. Instead of loading a static file, your web server will return the output of your python script.
This isn't very sophisticated, but if you are simply returning the output without needing browser submitted date, this is the easiest way (scaling under load is a different story).
You don't even need the "cgi" module from python, if you aren't receiving any data from the browser. Anything more complicated than this and you should use a web framework.
Examples and other methods
Simple Example: hardest part is webserver configuration
mod_python: Cut down on CGI overhead (otherwise, apache execs the python interpreter for each hit)
python module cgi: sending data to your python script from the browser.
Sorting
Javascript side sorting: I've used this javascript library to add sortable tables. This is the easiest way to add sorting without requiring additional work or another HTTP GET.
Instructions:
Download this file
Add to your HTML
Add class="sortable" to any table you'd like to make sortable
Click on the headers to sort
You might consider Tornado if Django is too much overhead. I've used both and agree that, if you have something simple/small to do and don't already know Django, it's going to exponentially increase your time to production. On the other hand, you can 'get' Tornado in a couple of hours and get something relatively simple done in a day or two with no prior experience with it. At least, that's been my experience with it.
Note that Tornado is still a tradeoff: you get a lot of simplicity in exchange for the huge cornucopia of features and shortcuts you get w/ Django.
PS - in addition to being a 'micro-framework', Tornado is also its own web server, so there's no mucking with wsgi/mod-cgi/fcgi.... just write your request handlers and run it. Be sure to see the demos included in the distribution.
Have you seen bottle framework? It is a micro framework and very simple.
If I correctly understood your requirements you might find Wooey very interesting.
Wooey is a A Django app that creates automatic web UIs for Python scripts:
http://wooey.readthedocs.org
Here you can check a demo:
https://wooey.herokuapp.com/
Django is a big webframework, meant to include loads of things becaus eyou often needs them, even though sometimes you don't.
Look at Pyramid, earlier known as BFG. It's much smaller.
http://pypi.python.org/pypi/pyramid/1.0a1
Other microframeworks to check out are here: http://wiki.python.org/moin/WebFrameworks
On the other hand, in this case it's probably also overkill. sounds like you can run the script once every ten minites, and write a static HTML file, and just use Apache.
If you are not willing to write your own tool, there is a pretty advanced tool for executing your scripts: http://rundeck.org/
It's pretty simple to start and can be configured for complex scenarios as well.
For the requirement of custom view (with sortable results), I believe you can implement a simple plugin for translating script output into html elements.
Also, for simple setups I could recommend my own tool: https://github.com/bugy/script-server. It doesn't have tons of features, but very easy for end-users and supports interactive execution.
If you don't need any input from the browser, this sounds like an almost-static webpage that just happens to change once a day. You'll only need some way to get html out of your script, in a place where your webserver can access it.)
So you'd use some form of templating; if you'll need some structure above the single page, there's static site / blog generators that you can feed your output in, say, Markdown format, and call their make html or the like.
You can use DicksonUI https://dicksonui.gitbook.io
DicksonUI is better
Or Remi gui(search in google)
DicksonUI is better.
I am the author of DicksonUI

How to create an internationalized Google App Engine application

I would like to provide my Python GAE website in the user's own language, using only the tools available directly in App Engine. For that, I would like to use GNU gettext files (.po and .mo files).
Has someone successfully combined Python Google App Engine and gettext files? If so, could you please provide the steps you used?
I had started a discussion in GAE's Google group, but haven't been able to extract from it how I'd like to do it: I don't want to add external dependencies, like Babel (suggested in the discussion). I want to use plain vanilla Google App Engine, so no manual update of Django or this kind of stuff.
At first, I will start using the language sent by the browser, so no need to manually force the language by using cookies etc. However, I might add a language changing feature later, once the basic internationalization works.
As a background note to give you more details about what I'm trying to do, I would like to internationalize Issue Tracker Tracker, an open source application I've hosted on Launchpad. I plan to use Launchpad's translation platform (explaining why I'd like to use .mo files). You can have a look at the source code in it's Bazaar branch (sorry no link due to stackoverflow spam prevention limit for new users...)
Thanks for helping me advance on this project!
As my needs were simple, I used a simple hack instead of (unavailable) gettext. I created a file with string translations, translate.py. Approximately like this:
en={}
ru={}
en['default_site_title']=u"Site title in English"
ru['default_site_title']=u"Название сайта по-русски"
Then in the main code I defined a function which returns a dictionary with translations into the most suitable language from the list (the first one to have a translation is used or English):
import translate
def get_messages(languages=[]):
msgs=translate.en
for lang in languages:
if hasattr(translate,lang):
msgs=getattr(translate,lang)
break
return msgs
Usage:
msgs = get_messages(["it","ru","en"])
hi = msgs['hello_message'] % 'yourname'
I also defined a helper function which extracts a list of languages from Accept-Language header.
It's not the most flexible solution, but it doesn't have any external dependencies and works for me (in a toy project). I think translate.py may be generated automatically from gettext files.
In case you want to see more, my actual source is here.
You can use the Django internationalisation tool, like explained here.
They are also saying that there is no easy way to do this.
I hope that helps you :)

Categories

Resources