Say I have some strings
"Open Youtube"
"Install PlayStore App"
"Go to Call of Duty app"
Now I have a rules.list file which contains all the rules in it to extract the named entity out of the above commands.
Say the contents of rules.list file is like this
app install (.*) 1
app install app (.*) 1
app install the (.*) app 1
app uninstall the app (.*) 1
app uninstall app (.*) 1
app uninstall the (.*) app 1
app go to (.*) app 1
app download (.*) 1
app download (.*) app 1
app download app (.*) 1
app download the app (.*) 1
app download the (.*) app 1
app install the app (.*) 1
app open the (.*) app 1
app open (.*) 1
app uninstall (.*) 1
app launch (.*) app 1
app launch (.*) 1
Is there any way I can use this rules.list file in python to extract the Named Entities from my sentences, so that I will have Youtube, PlayStore , Call of Duty as my output?
If you strip the rules from the start "app " and the end " 1" then you get a re-expression. The (.*) will return a group containing the wanted value.
A bit tricky are the capitals which you use in the strings but not in the rules.
Because of that I make the string lowercase before using re.
rules = [
"app install (.*) 1",
"app install app (.*) 1",
"app install the (.*) app 1",
"app uninstall the app (.*) 1",
"app uninstall app (.*) 1",
"app uninstall the (.*) app 1",
"app go to (.*) app 1",
"app install the app (.*) 1",
"app open the (.*) app 1",
"app open (.*) 1",
"app launch (.*) 1",
]
for rule in rules:
rule = rule[4:-1].strip()
# print(rule)
for string in strings:
result = re.search(rule, string.lower())
if result:
print('-----------------------------')
print(f'rule - {rule}')
print(f'string - {string}')
print(f'result - {result.group(1)}')
Output
-----------------------------
rule - install (.*)
string - Install PlayStore App
result - playstore app
-----------------------------
rule - go to (.*) app
string - Go to Call of Duty app
result - call of duty
-----------------------------
rule - open (.*)
string - Open Youtube
result - youtube
I think this should get you started.
Related
I'm using Gunicorn to deploy my Dash app. After upgrading to Gunicorn 20.0.0, it fails to find my application.
gunicorn --bind=0.0.0.0 --timeout 600 index:app.server
Failed to find application object 'app.server' in 'index'
[INFO] Shutting down: Master
[INFO] Reason: App failed to load.
This issue on Gunicorn's issue tracker seems to be related to the error, but I can't figure out what I'm supposed to do to fix it. How can I make Gunicorn 20 find my app?
index.py:
import os
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
from pages import overview
from webapp import app
app.index_string = open(os.path.join("html", "index.html")).read()
app.layout = html.Div([
dcc.Location(id="url", refresh=False),
html.Div(id="page-content")
])
#app.callback(Output("page-content", "children"), [Input("url", "pathname")])
def display_page(pathname):
if pathname == "/a-service/overview":
return overview.layout
else:
return overview.layout
if __name__ == "__main__":
app.run_server(debug=True, port=8051)
webapp.py:
import dash
description = "a description"
title = "a title"
creator = "#altf1be"
app = dash.Dash(
__name__,
meta_tags=[
{"name": "viewport", "content": "width=device-width, initial-scale=1"},
{"name": "description", "content": description},
{"name": "twitter:description", "content": description},
{"property": "og:title", "content": description},
{"name": "twitter:creator", "content": creator}
]
)
server = app.server
app.config.suppress_callback_exceptions = True
Gunicorn 20 changed how it parses and loads the application argument. It used to use eval, which followed attribute access. Now it only does a simple lookup for a single name in the given module. The ability for Gunicorn to understand Python syntax such as attribute access was not documented or intended.
Dash's docs about deployment don't use the syntax you're using though. They say to do the following, which will work for any version of Gunicorn:
webapp.py:
server = app.server
$ gunicorn webapp:server
You're already adding the server alias in the webapp module, but your code layout is a bit off and making things confusing for you. You're ignoring the setup you do in webapp and using index as your entry point instead. You're putting everything in separate top-level modules rather than within a package.
If you want to split your app setup from your index views, you should follow the standard Flask pattern of defining the app, then importing the views after that, all within a package.
project/
myapp/
__init__.py
webapp.py
index.py
webapp.py:
app = dash.Dash(...)
server = app.server
# index imports app, so import it after app is defined to avoid a circular import
from myapp import index
$ gunicorn myapp.webapp:server
you should also import server from webapp.py to index.py and then use regular gunicorn procedure:
gunicorn index:server -b :8000
A Dash project that is structured as a multi-page app (https://dash.plotly.com/urls) will have app.py (named as webapp.py here) and index.py. As mentioned in the linked guide, the entry point should be index.py to prevent circular import.
Only two changes are needed to use index.py as the entry point:
Import both app and server into index.py
from webapp import app, server
Run gunicorn as follows
gunicorn -b localhost:8000 index:server
Currently I am trying to a deploy my first FLASK application on PythonAnywhere.
Im not sure if this is the correct terminology but I have a folder as a module and there for I can't seem to find the correct way to deploy my application. I am not even sure where to start in resolving this issue. Any advice?
File and Folder Layout Snipped
my init.py code is:
import os
from flask import Flask
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='secret',
DATABASE=os.path.join(app.instance_path, 'LAMA.sqlite'),
)
if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
# database
from . import db
db.init_app(app)
# authentication blueprint
from . import auth
app.register_blueprint(auth.bp)
# blog blueprint - the main index
# from . import blog
# app.register_blueprint(blog.bp)
# app.add_url_rule('/', endpoint='index')
# book blueprint
from . import book
app.register_blueprint(book.bp)
app.add_url_rule('/', endpoint='index')
return app
I have also followed the python debugging page where I have done the following:
>>> import LAMA
>>> print(LAMA)
<module 'LAMA' from '/home/ivanv257/LAMA_MAIN/LAMA/__init__.py'>
So at this stage in my WSGI configuration file I have:
import sys
path = '/home/ivanv257/LAMA_MAIN/LAMA/__init__.py'
if path not in sys.path:
sys.path.append(path)
from LAMA import app as application
I have also tried many other combinations such as
path = '/home/ivanv257/LAMA_MAIN/LAMA/'
from init import app as application
path = '/home/ivanv257/LAMA_MAIN/'
from init import app as application
path = '/home/ivanv257/LAMA_MAIN/'
from LAMA import app as application
my source code path is : /home/ivanv257/LAMA_MAIN/LAMA , although I have also tried different combinations such as /home/ivanv257/LAMA_MAIN/
ERROR DETAIL:
2018-12-08 10:05:32,028: Error running WSGI application
2018-12-08 10:05:32,030: ModuleNotFoundError: No module named 'LAMA'
2018-12-08 10:05:32,030: File "/var/www/ivanv257_pythonanywhere_com_wsgi.py", line 83, in <module>
2018-12-08 10:05:32,030: from LAMA import app as application # noqa
To solve my problem I changed the following (with some assistance) from lama import create_app:
import sys
path = '/home/ivanv257/LAMA_MAIN/LAMA'
if path not in sys.path:
sys.path.append(path)
from lama import create_app
application = create_app()
I also had to remove the from . to just imports
import db
db.init_app(app)
# authentication blueprint
import auth
app.register_blueprint(auth.bp)
You are close. To deploy your app, navigate to the webapps page on your user dashboard. If you have not done so already, click the "Add new a webapp" button and enter the desired name of the app. Then, on the same webapp page on the dashboard, scroll down to the "Code" section of the page. Click on the "source code" a href and add the absolute path (full path) to the lama_main directory storing your init.py file.
Next, click on "WSGI configuration file" link. In the WSGI configuration file for your app, set the correct path to the parent directory and import app from the init.py file:
import sys
# add your project directory to the sys.path
project_home = u'/home/your_user_name/lama_main'
if project_home not in sys.path:
sys.path = [project_home] + sys.path
# import flask app but need to call it "application" for WSGI to work
from init import app as application #note that the module being imported from must be the file with "app" defined.
Then, save the WSGI file and return to the webapp panel on your dashboard. Click the "Reload {your site name}" button. Now, you should be able to visit the site by clicking on the main link at the top of the page.
I am using Flask to develop a python app. At the moment, I want this app to be run locally. It runs locally fine through python, but when I use cx_freeze to turn it into an exe for Windows, I can no longer use the Flask.render_template() method. The moment I try to execute a render_template, I get an http 500 error, exactly as if the html template I'm trying to render does not exist.
The main python file is called index.py. At first I tried to run: cxfreeze index.py. This did not include the "templates" directory from the Flask project in the cxfreeze "dist" directory. So then I tried using this setup.py script and running python setup.py build. This now includes the templates folder and the index.html template, but I still get the http: 500 error when it tries to render the template.
from cx_Freeze import setup,Executable
includefiles = [ 'templates\index.html']
includes = []
excludes = ['Tkinter']
setup(
name = 'index',
version = '0.1',
description = 'membership app',
author = 'Me',
author_email = 'me#me.com',
options = {'build_exe': {'excludes':excludes,'include_files':includefiles}},
executables = [Executable('index.py')]
)
Here is an example method from the script:
#app.route('/index', methods=['GET'])
def index():
print "rendering index"
return render_template("index.html")
If I run index.py then in the console I get:
* Running on http://0.0.0.0:5000/
rendering index
127.0.0.1 - - [26/Dec/2012 15:26:41] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [26/Dec/2012 15:26:42] "GET /favicon.ico HTTP/1.1" 404 -
and the page is displayed correctly in my browser, but if I run index.exe, I get
* Running on http://0.0.0.0:5000/
rendering index
127.0.0.1 - - [26/Dec/2012 15:30:57] "GET / HTTP/1.1" 500 -
127.0.0.1 - - [26/Dec/2012 15:30:57] "GET /favicon.ico HTTP/1.1" 404 -
and
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
in my browser.
If I return raw html, e.g.
#app.route('/index', methods=['GET'])
def index():
print "rendering index"
return "This works"
then it works fine. So a possible work around is to stop using Flask's templates and hardcode all the html logic into the main python file. This gets very messy though, so I'd like to avoid it if possible.
I'm using Python 2.7 32-bit, Cx_freeze for Python 2.7 32-bit, and Flask 0.9
Thanks for any help and ideas!
After many false trails trawling through the Flask and Jinga modules, I finally found the problem.
CXFreeze does not recognize that jinja2.ext is a dependency, and was not including it.
I fixed this by including import jinja2.ext in one of the python files.
CXFreeze then added ext.pyc to library.zip\jinja. (Copying it in manually after the build also works)
Just in case anyone else is mad enough to try use Flask to develop locally run apps :)
An alternative to import jinja2.ext in the source file is to specifically include jinja2.ext in the setup.py:
from cx_Freeze import setup,Executable
includefiles = [ 'templates\index.html']
includes = ['jinja2.ext'] # add jinja2.ext here
excludes = ['Tkinter']
setup(
name = 'index',
version = '0.1',
description = 'membership app',
author = 'Me',
author_email = 'me#me.com',
# Add includes to the options
options = {'build_exe': {'excludes':excludes,'include_files':includefiles, 'includes':includes}},
executables = [Executable('index.py')]
)
I've split handlers between 2 python files (main.py and main_cms.py). app.yaml defines the URLs that each python file will handle.
When I look at the Appstats, only the handlers from one of the 2 python files are profiled (the ones from main.py).
The 'magic' of webapp_add_wsgi_middleware(app) always used to work just fine, until the split. How can I make Appstats recording apply to all handlers?
appengine_config.py:
def webapp_add_wsgi_middleware(app):
from google.appengine.ext.appstats import recording
app = recording.appstats_wsgi_middleware(app)
return app
app.yaml:
builtins:
- appstats: on
...
- url: /services/.*
script: main.application
- url: /cms.*
script: main_cms.application
main.py and main_cms.py:
application = webapp2.WSGIApplication(url_mapping, config=config)
Running python2.7 / GAE 1.6.3, the dev_appserver.py shows Appstats correct for all handlers. The described problem is only seen in production.
How do i get a basic web2py server up and running on
PythonAnywhere?
[update - 29/05] We now have a big button on the web tab that will do all this stuff for you. Just click where it says Web2Py, fill in your admin password, and you're good to go.
Here's the old stuff for historical interest...
I'm a PythonAnywhere developer. We're not massive web2py experts (yet?) but I've managed to get web2py up and running like this:
First download and unpack web2py:
wget http://www.web2py.com/examples/static/web2py_src.zip
unzip web2py_src.zip
Go to the PythonAnywhere "Web" panel and edit your wsgi.py. Add these lines:
import os
import sys
path = '/home/my_username/web2py'
if path not in sys.path:
sys.path.append(path)
from wsgihandler import application
replacing my_username with your username.
You will also need to comment out the last two lines in wsgi.py, where we have the default hello world web.py application...
# comment out these two lines if you want to use another framework
#app = web.application(urls, globals())
#application = app.wsgifunc()
Thanks to Juan Martinez for his instructions on this part, which you can view here:
http://web2py.pythonanywhere.com/
then open a Bash console, and cd into the main web2py folder, then run
python web2py.py --port=80
enter admin password
press ctrl-c
(this will generate the parameters_80.py config file)
then go to your Web panel on PythonAnywhere, click reload web app,
and things should work!
You can also simply run this bash script:
http://pastebin.com/zcA5A89k
admin will be disabled because of no HTTPS unless you bypass it as in the previous post. It will create a security vulnerability.
Pastebin was down, I retrieved this from the cache.
cd ~
wget -O web2py_srz.zip http://web2py.com/examples/static/web2py_src.zip
unzip web2py_src.zip
echo "
PATH = '/home/"`whoami`"/web2py'
import os
import sys
sys.stdout = sys.stderr
os.chdir(PATH)
if not './' in sys.path[:1]: sys.path.insert(0,'./')
from gluon.main import wsgibase as application
" > /var/www/wsgi.py
cd web2py
python -c "from gluon.main import save_password; save_password(raw_input('admin password: '),433)"
I have recently summarized my experience with deployment of Web2Py on PythonAnywhere here
Hope it helps
NeoToren
I'll try to add something new to the discussion. The EASIEST way I've found is to go here when you aren't logged in. This makes it so you don't have to mess around with the terminal:
https://www.pythonanywhere.com/try-web2py
Come up with a domain name, then you'll get redirected to a page showing your login information and created dashboard for that domain. From there just create an account so your app isn't erased after 24 hours. When you sign up, your app has a 3 month expiry date (if you're not paying). I believe this is a new policy. Then simply go to https://appname.pythonanywhere.com/admin and then enter the password you were given and then upload your Web2Py file into the dashboard and then visit the page.
I'm not sure how to upload a Web2Py app on PythonAnywhere for an existing account, but that's the easiest method I've found.