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
Related
Specifically, I want to make a website/blog where people can track live data from some of my data-analysis scripts about stocks and such which I have made in Python. My idea for now is just to display some tables.
I'm quite novice with web hosting however, so is it possible to import the files directly into WordPress or Wix or something, or do I have to embed it within the HTML code?
I would prefer using Python because of all the libraries and the stock data API I use. Is it better to use Javascript though?
Lastly, I know about Django and Flask but the design of the website is very important for me too. That is why I want to use something like WordPress or Wix.
Background: Am comfortable in Python, know nothing about web deployment. I am looking into it as an alternative to compiling into .exe or .app for Win or Mac distributions.
Issue: I have a simple application that uses BeautifulSoup, openpyxl, and PySimplyGUI. It interacts with some local excel-files and creates new ones. I want to be able to, using minimum effort, make it accessible on my own web page or something similar, and make the created excel-files available for browsing/download. I have no idea how to do any of this. I've been looking into Flask and cloud foundry, but it feels like there should be some easy alternative that I'm missing. Ideally I would want a page where someone can log in (given a username and password I supply), which then directs to a page where the user can interact with my application.
Request: Is there a relatively easy way to do this that doesn't involve setting up a lot of stuff in html, etc., and where excel-files can still be interacted with by openpyxl? I ideally would just want some template, where I can "fill in the blanks" for the python method I would want to execute for each button!
Hope this makes sense. Thanks in advance :)
The easiest way to create a web app with a simple interface yet effective which does not require frontend programming is Streamlit. It is primarily used by data scientist to create simple web apps quickly.
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.
I've been looking into microframeworks for Python, and have come across two interesting options, Flask and Bottle. each have some similar features. One thing I noticed is that all the example sites show all the application code located inside a single Python file. Obviously, for even moderately sized sites, this would become difficult to manage quite quickly. Do either (or both) of these frameworks support being broken up among different files, and if so how would that be accomplished?
I'm familiar with Django, and like how its a little more structured, but i would rather use something more lightweight, but still powerful.
I don't have any experience with Bottle, but take a look at the Flask docs on larger applications. My Flask apps all use multiple Flask Module objects as that page recommends, one per Python module, and it seems to work just fine.
One thing that's nice about the Module objects is that you can customize dispatch on each one to create URL routing "domains" in your app. So for example, I'm trying to ape a Windows app in some of my code so I have a CaseInsensitiveModule that does case-insensitive dispatch, and I rigged up a RemoteModule to turn HTTP requests into Python methods using the Flask/Werkzeug routing system.
(Note that in current Flask versions, Modules are now Blueprints.)
I can't see how there could be any way of stopping this from working. Flask and Bottle, like Django, are just Python underneath, and Python allows you to break up files into modules. As long as you importing the relevant functions into the main script, they will just work exactly as if they were defined there.
I know a few people have started using my own article on doing this with Flask, although there are obviously other ways to do it depending on the size of the project; even I drop the directory type module for a file based one for smaller projects. Have a look at http://www.cols-code-snippets.co.uk/2011/02/my-take-on-flask-application-skeleton.html
I recently posted a sort of tutorial on how to get started with Bottle+Jinja2 in Google App Engine. My emphasis here is on how to organize the project files. You may be able to get something that you can use: http://codeaspoetry.wordpress.com/2011/11/27/how-to-build-a-web-app-using-bottle-with-jinja2-in-google-app-engine/
It really depends what you are trying to achieve, for micro service/applications/websites bottle is very straight forward and light weight. If you plan your application to grow by the time then Flask might be good option for you coz it has lot of extensions. We have about 40 to 50 micro services written in bottle and never faced any issues.
I'm just starting out with Python and have practiced so far in the IDLE interface. Now I'd like to configure Python with MAMP so I can start creating really basic webapps — using Python inside HTML, or well, vice-versa. (I'm assuming HTML is allowed in Python, just like PHP? If not, are there any modules/template engines for that?)
What modules do I need to install to run .py from my localhost? Googling a bit, it seems there're various methods — mod_python, FastCGI etc.. which one should I use and how to install it with MAMP Pro 1.8.2?
Many thanks
I think probably the easiest way for you to get started is to work with something like Django. It's a top-to-bottom web development stack which provides you with everything you need to develop and run a backend server. Things can be very simple in that world, no need to mess around with mod_python or FastCGI unless you really have the need.
It's also nice because it conforms to WSGI, which is a Python standard which allows you to plug together unrelated bits of reusable code to add specific functionality to your web app when needed (say for example on-the-fly gzip compression, or OpenID authentication). Once you have outgrown the default Django stack, or want to change something specific you can go down this road if you want.
Those are a few pointers to get you started. You could also look at other alternative frameworks such as TurboGears or paste if you wanted but Django is a great way to get something up and running quickly. Anyway, I'm sure you'll enjoy the experience: WSGI makes it a real joy knocking up web apps with the wealth of Python code you'll find on the web.
[edit: you may find it helpful to browse some of the may Django related questions here on stack-overflow if you run into problems]
You asked whether HTML is allowed within Python, which indicates that you still think too much in PHP terms about it. Contrary to PHP, Python was not designed to create dynamic web-pages. Instead, it was designed as a stand-alone, general-purpose programming language. Therefore you will not be able to put HTML into Python. There are some templating libraries which allow you to go the other way around, somewhat, but that's a completely different issue.
With things like Django or TurboGears or all the other web-frameworks, you essentially set up a small, stand-alone web-server (which comes bundled with the framework so you don't have to do anything), tell the server which function should handle what URL and then write those functions. In the simplest case, each URL you specify has its own function.
That 'handler function' (or 'view function' in Django terminology) receives a request object in which interesting info about the just-received request is contained. It then does whatever processing is required (a DB query for example). Finally, it produces some output, which is returned to the client. A typical way to get the output is to have some data passed to a template where it is rendered together with some HTML.
So, the HTML is separated in a template (in the typical case) and is not in the Python code.
About Python 3: I think you will find that the vast majority of all Python development going on in the world is still with Python 2.*. As others have pointed out here, Python 3 is just coming out, most of the good stuff is not available for it yet, and you shouldn't be bothered about that.
My advise: Grab yourself Python 2.6 and Django 1.1 and dive in. It's fun.
Django is definitely not the easiest way.
check out pylons. http://pylonshq.com/
also check sqlalchemy for sql related stuff. Very cool library.
On the other hand, you can always start with something very simple like mako for templating. http://www.makotemplates.org/