Forums

How to publish to an url from Pythonanywhere

Hi, I have a code that created csv file and can save it to html using df.to_html. It is a list of stocks data analysis. I want to publish it to a webpage after I run the python script. Most likely using the schedule functions. May I know how I can do it without manually upload the html file?

You could perhaps use PA's Static Files facility, see for example https://help.pythonanywhere.com/pages/StaticFiles, also search in these Forums.

Or you can use FTP in your Python code to copy the files to anywhere where you have suitable privileges and they're visible via an HTTP server.

@jgmdavies Thanks for reply. Do you mean use static files and the csv that I created is dynamic files? How can i use along with the webapps in pythonanywhere?

I believe from your description that your HTML files are 'static files' - as opposed to you generating web pages dynamically from a database or whatever. 'Static' here means that the HTML page(s) don't change as a result of user input - you as 'publisher' are updating them now and again.

I don't personally use PA's static files, but I'm sure other people here can help. Doesn't that link give you enough info?

HTH Jim

+1 to what Jim says.

@bkcollecton -- a "static file" is a file that you have stored on disk which you want to be sent to a visiting browser without modification. So, if your scheduled script is generating files that you want people to see, then they are "static" in that sense.

These are called static by contrast with pages that are generated on-the-fly by Python code that runs on every browser request.

Thanks Giles - you were clearer than me (as usual...).

I sometimes worry about the term 'static files', as usually it simply means 'files'. The alternative being typically HTML etc. content existing only in memory and passed as a response from e.g. a Python app invoked by a request to a web server - probably never stored to named disc files at all.

You're totally right, "static" is a misleading word -- the problem is that it seems to have stuck as the normal term for files like that, so probably best if we stick with it rather than creating a new standard...

lol

isn't that what standards are for (making new ones)?

Hi guys, thanks for the reply. I think it should be easier to set up by using webapps in pythonanywhere. flask or web2py. any command that can allow me to post straight away from my csv/html to usename.pythonanywhere.com?

@bkcollecton I don't know if it'll help you, but hopefully someone will find this helpful.

I just setup Static files for the first time following the guidance in that link I posted earlier. I used the Files tab in Dashboard to create a new root-level folder called 'public-html' and mapped it to a URL called /public/ using the Static files section on the Web tab of the Dashboard, then clicking the 'Reload' button on the Web tab. I ended up with:

URL           Directory
/public/      /home/jgmdavies/public-html

in the Static files section of my Web tab.

So to do something similar to what you want, I could simply get my Python code to create the HTML files in my 'public-html' folder, and then they would be visible to the world via http://jgmdavies.pythonanywhere.com/public (you won't be able to access this, as I've got a password set).

This is independent of any Web apps, i.e. you could create the files using a Python app running in a Bash shell, or in a Scheduled job (or using a Web app).

HTH

Jim

you can access the files after you setup the static file mapping.

eg: you can set it up so that you write all your csv files to /home/bkcollecton/my_csv_files and setup the static mapping so that anyone going to bkcollecton.pythonanywhere.com/csv/ would be able to access the files

this already works out of the box if you set up your static mappings on the webapps tab

@conrad, access the csv is view it as a webpage or download the file? I would like to be view as webpage.

@bkcollecton

I think you need something like your original plan, i.e. "... csv file and can save it to html using df.to_html". To 'publish' those HTML files, either create them in the folder you've mapped using the Static files functionality, or create them in some other folder and then copy them over.

when you view the csv in your browser, whether it is directly displayed within the browser as a comma seperated file, or whether it is downloaded, is something that is controlled by the viewer's browser settings.

if you want to force the csv to be something else (eg: an html table) then do your to_html conversion thing and then serve it from within a webapp. there is no straight forward solution for this. you can check if there are any third party apps that you can install and use.

@conrad, I know nothing much about html. Sorry of my confusion. I wish to show something like http://vindex.zohosites.com/data.html

I want the table like this able to publish from my csv file, is it possible?

If I want to publish something like in http://vindex.zohosites.com/data.html It is in table form and I want to publish my csv data in table form

Possible to publish in table form? I want to publish my csv data in table form so that it is easier to view.

This might help: http://www.ctroms.com/blog/code/python/2011/04/20/csv-to-html-table-with-python/

I have created the webapps using web2py. How can I upload my html file? Or how can I publish automatically each tie I create the html?

If you're using web2py, you edit your files from within the web2py admin interface.

@bkcollecton

Me again.

Do you mean you have Python code in a Web2Py app that can create the HTML files that you want to publish, using the source CSV data files as input?

If so, my answer remains:

I think you need something like your original plan, i.e. "... csv file and can save it to html using df.to_html". To 'publish' those HTML files, either create them in the folder you've mapped using the Static files functionality, or create them in some other folder and then copy them over.

HTH

Jim

Hi Jim,

I didn't have any new python code in web2py but I do have the html file and is link to the static files portion. Please refer the attachments below. enter image description here

Hi bkcollection,

Both parts of your Static files setup look wrong to me - please see my example above (March 26, 2016, 10:26 a.m).

The URL part should be the relative path you want for the URL, just 'public' in my example.

And the Directory part should point to the directory where you're writing your HTML files, not to one of the files. If you create a file there called index.html, it will be picked as the home page by default as per normal web server behaviour.

HTH

Jim