How to integrate a python file onto a Django project? - python

I am trying to include a machine learning component in my Django project. I have the method written in python. How do I get to make it work on the website using Django. Can I simply drag the ".py" file into the file structure and call it from an HTML page? I am new to Django any help would be greatly appreciated.

Yes you can directly copy file into your Django Directory structure. Let's say you have a file test.py and a function written in it as def print(). And you have copied the file in app directory. Then you can call it in views.py as from app.test import print. print function will be imported in views.py and you can use it to serve in html as you want.

Related

How can I run an html page from python using CGI

I am on a project where I have connected python and html using CGI. I am now able to receive inputs from html and save it to MySQL database through python. My project is a ticket booking website. I now have to show the user that their tickets have been booked, and also, I will have to show them their fare. Though I can simply type the code in printing statements to print the data in html, it doesn't look good from the user side, as I can't style it. Has anyone got any solutions? Can I directly start any html page like that?
I tried to print the fare, and I was able to do it. But the page looked very boring without any styling. I want to open an html page from my python file (because I can style it)-, but I am not using any web application like Flask or Django. How can I do it?
To run your Python script as a CGI script, you can either:
Name your script with a .cgi file extension (example.cgi).
Configure Apache to recognize and allow the .py file extension as a CGI script.
To run your python script as a CGI script,you can either:
Name your script with a .cgi file extension (example.cgi).
Configure Apache to recognize and allow the .py file extension as a CGI extension
"Run a HTML page" doen't make much sense. Sounds like your Python script is already running under CGI, and it's html output needs to include style information.
Print out the style information as well as the values into your html.
Or print out a link into the html header to include a seperate style sheet.
Or use python to load a html template from a file and make some string substitutions with your data before printing out the edited template.

import a file in a folder python

I want to import a file from a folder in my replit discord.py project. I’m using the same project to do a website , with flask. This is the structure
project ——|—— main.py
|
|—— static ——|—— Templates —— index.html
|
|—— flask_.py
I’m using a function who keep alive the bot using uptime robot. Before, I didn’t need a website. The flask app just print in a website output I’m alive. But now, I want to create a full website with flask using HTML. The HTML isn’t a problem. The problem is that, in the structure, you can see that flask_.py is in the folder static and the main.py file isn’t. Because the function to keep alive the bot is in flask_.py, I can’t import it like that from flask_ import keep_alive. I don’t know how to import it. Can you help me?
You need to start from the root directory of the project:
from static.flask_ import keep_alive
But why do you have have dynamic code in a folder named static? I suggest moving the flask_.py folder to the root of the project then you can use the import you already have.
Side note: in python, we typically use underscores to separate words in names. Ending a filename with an underscore is unusual.

Accessing app-specific server-side generated files in django template

I have a django app (my_app) that based on the user query:
creates a file from the db
runs a program on the file from step-1 and gets an output file
generates a json file from the step-2 output file
renders a D3 visualization from a django template using the data from the json file from step-3
I need the program to run on the server side and the json file to be generated server-side as well.
Because the json files are query-specific, I thought it's not a good idea to keep these files in the /static/ folder and thought of keeping the files (even if temporarily) in e.g. /myapp/output_files/ folder.
The problem is that there is no url pattern corresponding to /myapp/output_files/my_file.json and I get a "Page not found (404)" error if I try to open the generated file and it obviously doesn't load in the javascript code in the template.
Is there a better way to design the system?
If the design is ok, how can I access a json file in the app's folder from the django template? Do I need something in the urls.py?
P.S. Everything works fine if I change the json location to /static/ or its subfolder.
Just add the location to your STATICFILES_DIRS setting as shown here
However, you probably need to build a view function that can somehow return the json based on some parameter in the url. Static files are meant to stay static...

What is the best way in python/django to let users add files to the database?

I am trying to let users create html pages that they can view on my website. So the homepage would just have a place for file upload, then some script on my site would take the file and convert it into the html page and place it at mysite.com/23klj4d(identifying file name). From my understanding, this would mean that the urls.py file gets updated to route that url to display the html page of the file. Is it possible to let users do this? Where would I put that conversion script?
You could simply write static files to disc, and then serve them as static files. That would be easier, but it depends on your other requirements.
But, from what I understand in your question you'd need:
A form to upload
A upload handler itself which inserts into the db
A view that renders based on a path
Not sure about the urls.py entry. You'll want something in there to separate this content from the rest of your site, and you'll probably also want something to safeguard against the file extensions that you serve there.
note: this has security hole written all over it. I would be super careful in how you test this.

writing my first Trac macro

Ok, I've looked all over, and I think I'm doing this right, but I'm not getting any results. Is there anyone out there who's written Trac macros that can guide me through the first steps? Here's what I've written:
from trac.wiki.macros import WikiMacroBase
from genshi.builder import tag
class MyMacro(WikiMacroBase):
"""Proof of concept"""
revision = "$Rev$"
url = "$URL$"
def expand_macro(self, formatter, name, args):
return tag.b("Hello world.")
I've saved it as a .py file and put it in my Trac project's /plugins directory. Do I need to restart apache? Am I correct in expecting [[MyMacro]] to output a Hello world. on the page?
When creating macros using that format, Trac expects your class to be named "<name>Macro". For example, if you wanted a macro named JustASample, you would name the class JustASampleMacro. Since you named your class MyMacro, Trac thinks that you want your macro to be named My. Try using [[My]] on a wiki page and see if you get the output you're expecting.
After you copy the file into the plugins directory, you will indeed want to restart the web server. Before doing so, delete any .pyc files that were created for your plugin. Also, ensure that the file is readable by the account under which the web server runs.

Categories

Resources