I am attempting to make a dynamic website for a school project. The problem is it has to be on the school server and I can't use any webframeworks. I have searched through google and stackoverflow but I can't seem to get an answer.
I have tried the code that was provided here:
How to implement a minimal server for AJAX in Python?
It worked on the local server but how can I change it so that it would open on the school server. When I used those codes, the page won't load or an internal error shows. Can someone point me in the right direction?
Using a web framework in python does not necessary needs a system package installation (like running a sudo apt-get install python-something).
In the end python frameworks are just files like in your project, but you can install them system wide (like in the apt-get example) or ship them within your project (probably what you want). Take a look at virtual environment for creating a self contained environment and setuptools foi packaging the application and its dependencies
For implementing an ajax server directly in python without a wsgi container (apache, nginx, etc) I recommend using flask. It is very, very simple and very powerful
Related
I develop a python library that also includes a server as a feature. Currently, the server is written in pure javascript, but I'd like to migrate to using React with TypeScript.
What I am not sure of is how I can set up my library structure so that I can use React+TypeScript for development but have only the compiled output included in my python application so pip install can work.
I'm assuming I have to make sure the React public folder is included in the python library, but I am new to using React, so I'd appreciate some feedback or pointers to libraries that may already implement such a workflow that I can use as a template.
I'm working on creating my first "real" web app using Django.
Yesterday I learned I should be using a web server like Nginx to serve static files and pass off requests for dynamic content to my web app. I also learned that I need something like Gunicorn as the intermediary between the web server (Nginx) and my Django app.
My question is about virtualenv. It makes sense that we would contain app related software in it's own separate environment. What should I install in virtualenv, and what gets installed system wide? For example, in this guide we seem to install Python, Nginx and the database system wide (because they're installed before virtualenv is installed) while Django and Gunicorn are installed in virtualenv. It makes sense that Gunicorn would have to go in the virtualenv since its importing our python app, as explained here. Are the other things required to be installed system wide? Or can I pick either way? Is one way preferred over another?
Thanks!
Virtualenv is for managing Python libraries. It is not for managing Python itself, or for external services such as databases; it does however manage the Python libraries you use to access the database.
There's no room for confusion here, because there's simply no way to install Python itself or a database within a virtualenv.
I have my development environment setup on Win 7 like this:
Django development structure
Apache -server- C:\Program Files (x86)\Apache Software Foundation\Apache2.4
PostgreSQL -database- C:\Program Files\PostgreSQL\9.2
Django -framework- C:\Python27\Lib\site-packages\django
Python -code- C:\Python27
Project -root- C:\mysite
|----------apps
|----------HTML
|----------CSS
|----------JavaScript
|----------assets
I am attempting to keep this extremely simple to start out. There are 5 main directories each with a distinct purpose. All the code resides in the project folder.
compared to WAMP structure:
C:\WAMP
|----------C:\Apache
|----------C:\MySQL
|----------C:\PHP
|----------C:\www
I like how Apache, MySQL, and PHP all reside in a neat directory. I know to keep the root project OUTSIDE in another directory in Django for security reasons.
Is it fine that Apache, PostgreSQL, and Python are installed all over the place in the Django environment?
Did I miss a core Django component and/or directory?
Will deploying and scaling be a problem?
I want this to be a guideline for beginning Django web programmers.
I can answer the question one by one:
Is if fine that Apache, PostgreSQL, and Python are installed all over the place in the Django environment?
All over the place sounds weird but yes it is totally fine.
Did I miss a core Django component and/or directory?
No you don't miss anything, Django core is in site-packages folder already and your site code is mysite, which can be located anywhere you want.
Will deploying and scaling be a problem?
No it won't be a problem with current structure. You will deploy your mysite only, the other will be installed separately.
Something you should get familiar with when starting with Django development:
Most likely when you deploy your project, it will be on a Linux server, so install and learn Linux maybe?
virtualenv: Soon you will have to install Django, then a bunch of external packages to support your project. virtualenv helps you isolate your working environment. Well it's "unofficial" a must when you start with python development.
virtualenvwrapper to make your life easier when working with virtualenv
git and github or bitbucket: if you don't know git yet, you should now.
Apache is just web server, it is used to serve files, but to make a website you do not necessary need it. Django comes with its own development server. See :
python manage.py runserver
Apache is required when you are developing PHP websites because your computer do not know how to compile and interpret it. But for Django, you use the Python language, and you have already install it if you are using Django.
Read https://docs.djangoproject.com/en/1.5/intro/tutorial01/
And where it will be the time to set up your own server using Apache look at :
https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/.
Scaling will be a problem on windows. Python in Apache on windows gets 64 threads in one process. Couple this with the GIL and you will have scaling issues.
Python and Apache on Linux don't have this same problem. Under Linux wsgi can create multiple processes that have multiple threads each, minimizing GIL issues.
WSGI in Apache on windows is not a scalable solution in my opinion.
However you can develop there and move to linux for deployment, I do it all the time.
You will want to take advantage of the Apache Alias directive to serve all your static content like css, js, favicon.ico. This frees up python to only handle requests that require logic.
I am creating a Python application that uses multiple third party libraries. Since the libraries are installed on my computer, the script runs fine. However, how can I alter my script so that it will run on any computer (all major OS), even if the computer does not have the third party Python libraries installed?
By your comment:
I want the script to stay a python script if at all possible so that
it can be run on any device and run through a webpage
It appears you want some way to host a python program online.
To do this, you need:
To know how to write Python that serves a website (see Django, Flask, CherryPy, etc...)
Some way to deploy said application to the web. An easy, free (<-- this is the keyword) way to deploy Python web apps is through using Heroku or some other free hosting site. Or you could always pay for hosting or host it yourself.
Following up on my last year's question on documentation, I now want to get started and try out Python-based Sphinx for putting together the developer documentation for a PHP CMS I've been working on.
Instead of setting up Python locally on my workstation, I would like to run it on a publicly accessible web server from the start. All the web hosting packages I have access to run on the LAMP stack, and I'm reluctant to buy Python-based hosting. I am very interested in the Google App Engine, the free quotas they provide will do for me a hundred times over, and even if not, their pricing looks very reasonable.
Now I have zero knowledge of Python - getting Sphinx to work would be my first contact with it - and very little time. As far as I understand, the platform and python libraries the App Engine provides are very compatible to a standard Python library but not identical.
So my question is:
Can Sphinx run on App Engine at all?
Is installing Sphinx on the App Engine as straightforward as if I would install it on top of a normal Python installation? Or will the App Engine's environment require tweaking of the source code that I can't perform in reasonable time with my current level of Python?
Should I be installing Sphinx on a local server and a "normal" Python stack instead first?
Does anybody know any helpful How-to's, tutorials or other resources for this?
You do not need to install Sphinx on GAE at all.
You use Sphinx to create a directory of static HTML, CSS and JS. When this step is finished, you simply upload the output from Sphinx -- in it's entirety.
The output from Sphinx (HTML, CSS and JS) is simply served from one place. You upload the documentation from where you created it to GAE as static files and serve them. Done.
There's no "install on a web server" aspect to Sphinx at all. Sphinx is not a web application; it does not run on your web server. You run it in development briefly to publish the documentation to HTML, LaTeX or whatever. Once you've created the static HTML files, you no longer need any part of Sphinx anywhere.
Can Sphinx run on App Engine at all?
I suppose it can, but you never need to.
Is installing Sphinx on the App Engine
as straightforward as...
It doesn't matter, because you don't install it there. You install it on your workstation and use it on your workstation.
Should I be installing Sphinx on a
local server and a "normal" Python
stack instead first?
Don't install it on a server. Install it on your workstation.
Does anybody know any helpful
How-to's, tutorials or other resources
for this?
If by "this" you mean "installing on a web server", then then answer is "no". One does not install it on a web server. So there are no how-to's, tutorials or resources for "installing on a web server".
If by "this" you mean "creating documentation with Sphinx", then the answer is "what's wrong with https://www.sphinx-doc.org? What do they lack?
I would like Sphinx's "engine" that
turns the input (consisting of reST
files) into HTML/CSS/JS to be
accessible from anywhere to make me
(and possible other contributors)
independent from a specific
workstation.
Sphinx is like a compiler. Everyone has their own copy on their workstation. They download the document source, make changes, commit the source changes, and upload the resulting document.
serve the generated documentation from
the same place.
Correct. After you download the source, make changes, regenerate the document and commit the changes, you upload the resulting document so it will be served from one -- and only one -- place.
Sphinx is a compiler. It is not a "web engine". It's a simple compiler that simply compiles your documentation into HTML so it can be served.
Interesting project! The main issue you're going to run into is that of filesystem access: The filesystem on App Engine is read-only, and based on looking at the source, Sphinx is fairly hard-coded to use the filesystem for output. It also expects to read the configuration file and input files from the local filesystem, which would make building docs for anything other than projects bundled with the app tricky.
It is possible to work around this, but it would require writing a simple virtual filesystem that uses the App Engine datastore, and using monkeypatching to make it work with the regular Python file interface. That's rather advanced for a "new to Python" project!
One other thing you might want to consider if you were pondering allowing users to upload projects to be documented: The Sphinx configuration file is in Python, so executing it could be dangerous - a user could do nasty things to your app with a malicious configuration file!
The difference between deployment locally via dev_appserver and remotely via appcfg has been - in my experience - limited to which of the two commands I execute. That said, I've no experience with Sphinx.
Sphinx compiles documentation, running it on a webserver makes as much sense as running gcc on a webserver.