Django Export data from postGre to shapeFile - python

I'm new in django and python.
I've tried to export database data to shapefile using https://bitbucket.org/springmeyer/django-shapes/src
and i got a Segmentation fault. is there is anyway to make this work by only using django GeoDjango?

That's a non-updated project. You can check this a bit more up to date fork.
To check quickly if it works for you download the zip and copy the shape-engine folder inside your django project.
Install fiona (pip install fiona)
Add a new url to download the shapfile like
from .views import export
urlpatterns = urlpatterns + [ url(r'^worldshapes/', export_worldshapes), ]
And a new view:
from shape_engine.shape_responder import ShpResponder
def export(request):
from .models import WorldBorders
w = WorldBorders.objects.all()
shp_response = ShpResponder(w)
shp_response.file_name = 'World Borders'
return shp_response()
django-shape-engine only works in python 2.x. A couple of changes should be made to work with it in python 3. Basically, use BytesIO instead of StringIO

Related

embedding jupyter notebook/ google colab in Django app

I wanted to build a website and embed the jupyter notebook functionality of being able to create cells and run python code within it into my website
For creating a website I m using Django
and I would like to embed either the google collab or jupyter notebook
By the way I have researched enough and have been stuck with the StackOverflow links where there no answer about this or the one where they want to use django in jupyter notebook
Thanks in advance for any guidance or any reference that you guys can provide.
You have many options to do that:
Note:: I used "jupyter-lab" you can use "jupyter notebook"
1- The first option to redirect to "jupyter notebook"
django view.py
from django.shortcuts import redirect,HttpResponse
import subprocess
import time
def open_jupiter_notbook(request):
b= subprocess.check_output("jupyter-lab list".split()).decode('utf-8')
if "9999" not in b:
a=subprocess.Popen("jupyter-lab --no-browser --port 9999".split())
start_time = time.time()
unreachable_time = 10
while "9999" not in b:
timer = time.time()
elapsed_time = timer-start_time
b= subprocess.check_output("jupyter-lab list".split()).decode('utf-8')
if "9999" in b:
break
if elapsed_time > unreachable_time:
return HttpResponse("Unreachable")
path = b.split('\n')[1].split('::',1)[0]
#You can here add data to your path if you want to open file or anything
return redirect(path)
if you want to implement it in template instead of redirect, you can use the following code in Django template:
<iframe src="{% url 'open_jupiter_notbook' %}" width= 600px height=200px></iframe>
2- The second option:
just use jupyter notebook commands
by using this subprocess.check_output("your command".split())
Export it first and then import it, look here for a good explanation
from google.colab import files
src = list(files.upload().values())[0]
open('mylib.py','wb').write(src)
import mylib

Autoplotter Python Library Integraton with Django Python Web framework

I have seen a tutorial here Which is demonstrating the data analysis in the jupyter notebook cell, I am looking for the solution that how can i show the output of autoplotter which is python library in the django templates. Below is the code snippet of autoplotter which i have taken from its official website:
from autoplotter import run_app # Importing the autoplotter for GUI Based EDA
import pandas as pd # Importing Pandas to read csv
df = pd.read_csv("https://raw.githubusercontent.com/ersaurabhverma/autoplotter/master/Data/data.csv") # Reading data
run_app(df,mode = "inline", host="127.0.0.1", port=5000) # Calling the autoplotter.run_app in inline mode
run_app(df,mode = "external", host="127.0.0.1", port=5000) # Calling the autoplotter.run_app in external mode
I am looking for that what is the output format of this command
run_app (dataframe, host, port....)
how can I display its output in my django templates? so that a person could interact with his data through my launched website? Looking for any smart solution. Thank you
You can't run it under Django project because autoplotter uses Flask to serve the data and Flask can work only in the main thread of the main interpreter.
However, you can solve your problem using Docker. You will have a separate service that serves the autoplotter app and Django can have an iframe in HTML template that shows the content of the service.
UPD:
For the Docker - you can start with this guide. The only difference will be in your case is that app.py will contain only the call for a plotter:
if __name == '__main__':
run_app(df, mode="external", host="127.0.0.1", port=5000)
And requirements.txt with autoplotter

How to load jinja template directly from filesystem

The jinja API document at pocoo.org states:
The simplest way to configure Jinja2 to load templates for your application looks roughly like this:
from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('yourapplication', templates'))
This will create a template environment with the default settings and a loader that looks up the templates in the templates folder inside the yourapplication python package.
As it turns out, this isn't so simple because you have to make/install a python package with your templates in it, which introduces a lot of needless complexity, especially if you have no intention of distributing your code.
I found these related questions about doing so, but the answers are vague and unsatisfying:
need to package jinja2 template for python
How to make a python package containing only jinja templates
How can I load the template directly from the filesystem, not as a resource in a package?
Use a FileSystemLoader instead of a PackageLoader. Suppose there is a python file in the same directory as the template:
./index.py
./template.html
This index.py will find the template and render it:
#!/usr/bin/python
import jinja2
templateLoader = jinja2.FileSystemLoader(searchpath="./")
templateEnv = jinja2.Environment(loader=templateLoader)
TEMPLATE_FILE = "template.html"
template = templateEnv.get_template(TEMPLATE_FILE)
outputText = template.render() # this is where to put args to the template renderer
print(outputText)
In the introduction, the PackageLoader approach seems to be presented as the default, "simplest" method; however, there is also a section which discusses all the built-in loaders.
A simpler way is to directly call the jinja2.Template constructor and use open to load the file:
from jinja2 import Template
with open('template.html.jinja2') as file_:
template = Template(file_.read())
template.render(name='John')
Here is the one liner:
from jinja2 import Template
with open('template_file.j2') as f:
template = Template(f.read())
Then you can render the template on another line, or for all in one line:
with open('template_file.j2') as f:
rendered = Template(f.read()).render(var="TEXT")
If using Python 3.4+ and Jinja2 - v2.11+ -- we can combine python's pathlib and Filesystem to simplify the flow
from pathlib import Path
...
p = Path(__file__).parent.parent / 'templates' # sample relative path
env = Environment(
loader=FileSystemLoader(Path(p)))
template = env.get_template('your_file.jinja2')
I am not comfortable with using directly Template(file) since Jinja's template inheritance processing may not work well.
Pathlib support is only added in latest version of Jinja - v2.11+
from jinja2 import Environment, select_autoescape, FileSystemLoader
env = Environment(loader=FileSystemLoader(
searchpath=folder_contain_list_html), autoescape=select_autoescape(['html', 'xml']))
template = env.get_template('file_name_detail_template')
body_html = template.render(**args)
send_email(body_html)

Django: Improperly configured url: unbalanced parenthesis

I'm developing a website ina VM on my Mac which then gets deployed to a remote UAT server. The VM is set up with the same OS and software stack as the UAT and live servers. I'm getting the following error when attempting to access the UAT version of my website:
"^accounts/update-user-group/(?P<pk>\d" is not a valid regular expression: unbalanced parenthesis
On first look it seems pretty obvious what's wrong: The given URL pattern is incomplete. However, my urls.py file has the correct full url:
# -*- coding: utf-8 -*-
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.conf.urls import patterns, url
from views import UserGroupList, UserGroupDetail
from views import UserGroupCreate, UserGroupUpdate, UserGroupDelete
from views import UserDeletedGroups, RecoverDeletedGroup
urlpatterns = patterns('',
url(_(r'^accounts/create-user-group/$'), UserGroupCreate.as_view(), name='user_group_create'),
url(_(r'^accounts/update-user-group/(?P<pk>\d+)/$'), UserGroupUpdate.as_view(), name='user_group_update'),
url(_(r'^delete/(?P<pk>\d+)/$'), UserGroupDelete.as_view(), name='user_group_delete'),
url(_(r'^accounts/user-group-deleted/$'), UserDeletedGroups.as_view(), name='user_group_deleted_list'),
url(_(r'^recover/(?P<pk>\d+)/$'), RecoverDeletedGroup.as_view(), name='user_group_recover_deleted'),
url(_(r'^accounts/user-group-details/(?P<pk>\d+)/$'), UserGroupDetail.as_view(), name='user_group_detail'),
url(_(r'^accounts/user-group-list/$'), UserGroupList.as_view(), name='user_group_list'),
)
So the error seems to be getting generated by the second regex in the urlpatterns. However, If I change the regex to this:
url(_(r'^accounts/update-user-group/(?P<pk>[\d]+)/$'), UserGroupUpdate.as_view(), name='user_group_update'),
Then the error moves on to the next line. All I've done here is add square brackets round the \d
for the pk argument. How can this simple difference be the cause of the error? And why would it only be happening in my UAT environment and not local development?
Daniels question pointed me in the right direction for this. The translation for the problem URL in my PO file was incomplete so ensuring that was correct sorted my problem.

Django JSON_RPC doesn't work

i want to create a simple Django(1.3) project that uses JSON-RPC. i use this implementation:
django-json-rpc
and this is my project files:
urls.py:
from django.conf.urls.defaults import patterns, include, url
from myapp.views import *
from jsonrpc import jsonrpc_site
urlpatterns = patterns('',
url(r'^json/browse/', 'jsonrpc.views.browse', name="jsonrpc_browser"),
url(r'^json/', jsonrpc_site.dispatch, name="jsonrpc_mountpoint"),
(r'^json/(?P<method>[a-zA-Z0-9.]+)$', jsonrpc_site.dispatch),
)
views.py:
from jsonrpc import jsonrpc_method
#jsonrpc_method('sayHello')
def hello(request, name='Lester'):
return "Hello %s" % name
when i test this code in JSON-RPC Browser(included with the library) it doesn't work. whe is want to add this import in the shell:
from jsonrpc.proxy import ServiceProxy
i get the response like this:
Error:
what's the problem here? it seems a simple process but it doesn't work for me.
i found the solution. in fact json-rpc works but in JSON-RPC Browser i have to treat with some difference than regular way. according to here, we should initialize and call json-rpc methods like this:
from jsonrpc.proxy import ServiceProxy
s = ServiceProxy('http://localhost:8080/json/')
s.myapp.sayHello('Sam')
but it's not true! this method is correct when we use it in django shell or in our main code! in JSON-RPC Browser we just need to call our method like this:
jsonrpc.sayHello('sam')
just that!
thanks to all.

Categories

Resources