Sorry to bother but I was trying to add syntax but it’s not working. I get this error →
Traceback (most recent call last):
File "/home/runner/Vial/app.py", line 5, i n ‹module>
from sre import App, route
File "/home/runner/Vial/src/__init__.py",
Line 3, in <module>
from . framework import App, route
ImportError: cannot import name 'route' from
' src.framework' (/home/runner/Vial/src/fram
ework.py
This is my code
__init__.py
→
# src/__init__.py
from .framework import App, route
__all__ = ['App', 'route']
framework.py
→
# framework.py
from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple
class Request(Request):
pass
class Response(Response):
pass
class App:
def __init__(self):
self.routes = {}
def route(self, path):
def decorator(view_func):
self.routes[path] = view_func
return view_func
return decorator
def dispatch_request(self, request):
view_func = self.routes.get(request.path)
if view_func is not None:
return view_func(request)
return Response('Not Found', status=404)
def __call__(self, environ, start_response):
request = Request(environ)
response = self.dispatch_request(request)
return response(environ, start_response)
def route(self, rule, **kwargs):
def decorator(f):
kwargs['endpoint'] = f.__name__
self.url_map.add(Rule(rule, **kwargs))
return f
return decorator
app.py
→
# app.py
from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple
from src import App, route
class MyApp(App): # Inherit from the App class you're importing
def __call__(self, environ, start_response):
request = Request(environ)
# Create a paragraph with a div inside using the HTML generator classes
p = P().attribute("class", "paragraph").child(Div())
# Convert the Tag object to a string to get the HTML markup
html_content = str(p)
# Set the content type header to text/html
response = Response(html_content, mimetype='text/html')
return response(environ, start_response)
if __name__ == '__main__':
app = MyApp()
run_simple('0.0.0.0', 5000, app)