Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Python - check if condition is met within a given timeout
I've a script that 1. downloads & push binaries to internal registry (func_to_push_image) and 2. process messages from a section of messaging queue, to see if the push is successful by filtering the messages for a keyword 3. If the push is successful, monitor and process another section of messaging queue, to validate whether those two binaries are installed by looking at the install_status keyword. Timeout for the push action is 2 mins, else fail the script immediately. Timeout for the install action is 1 min, else fail the script with timeout. Messaging queue's output for the binaries installations looks like below. { "location": "root", "md5":"a3288ec45c8d159fbb656972eb4cdfg84jsfbsdf", "fireEye":"v3", "Binaries":[ A:{ "name":"HP-Protect", "version": "v1.3", "effect": "NIL", "install_status":"On Going", } B:{ "name":"Covertur", "version": "v1.0", "effect": "NIL", "install_status":"Installed" }] } Redacted code: registry_timeout = 2 install_timeout = 1 Other variables holding server and partition details. def get_info_from_messaging_queue(server, broker): depending on the partition, returns the messages from the messaging queue. def func_to_push_image(): logic to upload the artifact to the registry. def func_to_check_status_of_installed_binaries(): #This function should Ideally check the status of both binaries has been "Completed" status = False install_status = get_info_from_messaging_queue(server, broker) if install_status['Binaries'][A]['install_status'] == "Completed" and install_status['Binaries'][B]['install_status'] == "Completed": status = True return status … -
Hide Unsupported operand type(s) for -: 'int' and 'NoneType' when no data in database
I am having this issue where I get this error unless there are at least 2 lines of data in the database. Basically what I am doing is marking an item as refunded. I will aggregate all of the fields and then do the math. If I do not have at least 2 lines of data in the database, I always get this error. unsupported operand type(s) for -: 'int' and 'NoneType' The specific line is throwing the error is: total_partial = 0 - total_p_r['refund_amount__sum'] I am not understanding why I am getting NoneType when the default is set to zero on all lines of the models. The code is below. views.py def inventory_management_refund(request): inventory = Inventory.objects.filter(Q(status__contains='REFUNDED') | Q(status__contains='Partial Refund')).order_by('id') totals = Inventory.objects.filter(status__contains='REFUNDED').aggregate( Sum('paid'), Sum('soldprice'), Sum('shipcost'), ) total_p_r = Inventory.objects.filter(status__contains='Partial Refund').aggregate( Sum('refund_amount') ) total_full = 0 - totals['paid__sum'] - totals['soldprice__sum'] - totals['shipcost__sum'] total_partial = 0 - total_p_r['refund_amount__sum'] total_refund_loss = total_full + total_partial return render(request, 'inventory/refunds.html', {"inventory": inventory, "total_refund_loss": total_refund_loss}) template.html <div> <div> <h1 class="text-center">Refunded Items</h1> <hr> </div> {% if inventory.count == 0 %} <h2 class="text-success text-center">There are currently no refunds!</h2> {% endif %} {% if inventory.count > 0 %} <table class="table table-striped"> <thead> <tr> <th>Product</th> <th>Description</th> <th class="invmanrow">Purchase Price</th> <th … -
Label with text copy functionality creation in tkinter
I've got the following tkinter classes that give copy/paste functionality to Entries and copy functionality to Labels. However, I'm not sure how to get the CPLabel class to retrieve the text from the text parameter. When I run the program as is, the CPLabel is packed as evidenced by a space where it should appear, however, no text shows. class CPEntry(Entry): def __init__(self, *args, **kwargs): Entry.__init__(self, *args, **kwargs) self.bind('<Control-c>', self.copy) self.bind('<Control-v>', self.paste) def copy(self, event): self.clipboard_clear() selected_text = self.selection_get() self.clipboard_append(selected_text) def paste(self, event): self.delete(0, END) pasted_text = self.clipboard_get() self.insert(0, pasted_text) class CPLabel(CPEntry): def __init__(self, *args, **kwargs): CPEntry.__init__(self, *args, **kwargs) self.configure(state="readonly", highlightthickness=0, bd=0, cursor="arrow", textvariable=None) self.insert(0, kwargs.get('text', '')) The text that should be displayed is what is within the text parameter. For example, the CPLabel below should display 'Enter a prime number for the value of p'. pLabel = CPLabel(root, text = 'Enter a prime number for the value of p') If you could let me know what I'm doing wrong, or how to get this to work properly that would be awesome. Thanks. -
How to make our program run when my raspberry pi boot up? either using Cron or rc.local
I want to send data from the sensor to the database using raspberry pi. The database is the raspberry pi itself. This is the code I want to run. import time import mysql.connector from bmp280 import BMP280 from datetime import datetime # DATABSE VARIABLE DBHOST = "localhost" #The Host use "localhost" or the database IP DBUSER = "ISTTS" #The user DBPASS = "digital123" #The user password DBDATA = "exampledb" #The database to insert DBTABL = "Test2" #The table to insert # DEVICE ID (int) DID = 1 # SENSOR DELAY DELAY = 5 try: from smbus2 import SMBus except ImportError: from smbus import SMBus mydb = mysql.connector.connect(host=DBHOST, user=DBUSER, password=DBPASS, database=DBDATA) mycursor = mydb.cursor() # Initialise the BMP280 bus = SMBus(1) bmp280 = BMP280(i2c_dev=bus) while True: now = datetime.now() temperature = round(bmp280.get_temperature(),2) pressure = round(bmp280.get_pressure(),2) print('{}*C {}hPa'.format(temperature, pressure)) time.sleep(DELAY) sql = "INSERT INTO " + DBTABL + " (Date, DeviceID, Temperature, Pressure) VALUES (%s, %s, %s, %s)" val = (now, DID, temperature, pressure) mycursor.execute(sql, val) mydb.commit() I tried to use cron before, but somehow it didn't work. What i put in my Cron is @reboot sudo python3 /home/istts/Desktop/MySQL.py I tried to see the log but didn't find anything related to my … -
why does function does not stop when a exception is raised (Python)
I have a class like this: import threading import time import psutil class TimeoutError(Exception): pass class Performance: def __init__(self, func, time_limit=20, except_return = None) -> None: self.func = func self.except_return = except_return start_time = time.perf_counter() process = psutil.Process() process.memory_info().rss process.memory_info().rss elapsed_time = time.perf_counter() - start_time self.time_limit = time_limit + elapsed_time self.__init_time_waste = elapsed_time def handler(self): with open('Output.txt',mode='a',encoding='utf-8') as f: f.writelines("Time Limited Exceeded!\n") raise TimeoutError("Time Limited Exceeded!") def run(self): def run_func(): timer = threading.Timer(self.time_limit,self.handler) result = [] start_time = 0 before_memory = 0 after_memory = 0 elapsed_time = 0 timer.start() try: start_time = time.perf_counter() process = psutil.Process() before_memory = process.memory_info().rss result = self.func() after_memory = process.memory_info().rss elapsed_time = time.perf_counter() - start_time - self.__init_time_waste except Exception as e: print(e) timer.cancel() return self.except_return else: timer.cancel() with open('Output.txt',mode='a',encoding='utf-8') as f: f.writelines(f"Executing Time: {elapsed_time:.5f} \n") f.writelines(f"Memory Used: {after_memory - before_memory} bytes \n") print(f"Executing Time: {elapsed_time:.5f}") print(f"Memory Used: {after_memory - before_memory} bytes") return result return run_func() This class is used to limit the run time of a function and compute the memory used. But when the exception is raised, the self.func still execute and doesn't terminated. Please help me :((( -
How to create a stub Python package for PyCharm?
Let's say we have a builtin module car. We have no autocomplete in PyCharm for it. It has interface: def getState(id, optional_id = None): pass def log(value): pass So, end usage would be: import car car.getState(5) car.log('Test') How to create a package and join it into PyCharm to make autocomplete work for a described interface? If I created a package car-stub with car.py file, and installed it in the project, I will have to use code line from car-stub import car. But how to make it in such a way it would not require code changes? The system would provide autocomplete for import car. -
Django backend server runtime error using gunicorn
I developed backend server using django python framework. But, It didn't work. I have searched this error in websites, but I couldn't find answer. Here is error log: 11:58:38 master systemd[1]: Starting Gunicorn Service... 11:58:38 master systemd[1]: Started Gunicorn Service. 11:58:38 master gunicorn[2705621]: [2023-03-20 11:58:38 +0900] [2705621] [INFO] Starting gunicorn 20.1.0 11:58:38 master gunicorn[2705621]: [2023-03-20 11:58:38 +0900] [2705621] [INFO] Listening at: http://127.0.0.1:8001 (2705621) 11:58:38 master gunicorn[2705621]: [2023-03-20 11:58:38 +0900] [2705621] [INFO] Using worker: eventlet 11:58:38 master gunicorn[2705626]: [2023-03-20 11:58:38 +0900] [2705626] [INFO] Booting worker with pid: 2705626 11:59:08 master gunicorn[2705621]: [2023-03-20 11:59:08 +0900] [2705621] [CRITICAL] WORKER TIMEOUT (pid:2705626) 11:59:08 master gunicorn[2705626]: Stack trace (most recent call last): 11:59:08 master gunicorn[2705626]: #16 Object "[0x7fd822b8c5b7]", at 0x7fd822b8c5b7, in 11:59:08 master gunicorn[2705626]: #15 Object "/home/master/envs/guide/lib/python3.8/site-packages/greenlet/_greenlet.cpython-38-x86_64-linux-gnu.so", at 0x7fd907a96cbf, in greenlet::UserGreenlet::g_switch() 11:59:08 master gunicorn[2705626]: #14 Object "/home/master/envs/guide/lib/python3.8/site-packages/greenlet/_greenlet.cpython-38-x86_64-linux-gnu.so", at 0x7fd907a969a1, in greenlet::UserGreenlet::g_initialstub(void*) 11:59:08 master gunicorn[2705626]: #13 Object "/home/master/envs/guide/lib/python3.8/site-packages/greenlet/_greenlet.cpython-38-x86_64-linux-gnu.so", at 0x7fd907a95a16, in greenlet::UserGreenlet::inner_bootstrap(greenlet::refs::_OwnedGreenlet<_greenlet, &greenlet::refs::GreenletChecker>&, greenlet::refs::OwnedReference<_object, &greenlet::refs::NoOpChecker>&) 11:59:08 master gunicorn[2705626]: #12 Object "/home/master/envs/guide/bin/python", at 0x5f52b1, in PyObject_Call 11:59:08 master gunicorn[2705626]: #11 Object "/home/master/envs/guide/bin/python", at 0x50b32b, in 11:59:08 master gunicorn[2705626]: #10 Object "/home/master/envs/guide/bin/python", at 0x5f60c2, in _PyFunction_Vectorcall 11:59:08 master gunicorn[2705626]: #9 Object "/home/master/envs/guide/bin/python", at 0x569d89, in _PyEval_EvalCodeWithName 11:59:08 master gunicorn[2705626]: #8 Object "/home/master/envs/guide/bin/python", at 0x56bbe0, in _PyEval_EvalFrameDefault 11:59:08 master … -
JSONDecodeError Expecting value: line 1 column 1 (char 0) when receiving via Bluetooth
I am transmitting a JSON payload from an Arduino microcontroller and attempting to receive it using a Python script: import bluetooth #pybluez import json sensor_address = "18:D9:31:YY:C7:4A" socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM) socket.connect((sensor_address, 1)) buffer = "" print("Listening...") while True: data = socket.recv(1024) buffer += str(data, encoding='ascii') print(buffer) # used to check json payload try: data = json.loads(buffer) print("Received:", data) buffer = "" except json.JSONDecodeError as e: print(e) continue Examining the value stored in buffer before entering the try statement, I see what appears to be perfectly valid JSON: {"a_x":957.5195,"a_y":-0.488281,"a_z":315.918,"g_x":-0.625954,"g_y":-1.305344}{"a_x":961.914,"a_y":-1.953125,"a_z":297.8516,"g_x":-2.816794,"g_y":2.572519}{"a_x":964.8437,"a_y":3.417969,"a_z":303.2227,"g_x":-1,"g_y":0.374046} However the result of my script is only Expecting value: line 1 column 1 (char 0) repeatedly. Why doesn't the code inside the try statement execute once a complete payload has been received? My hunch is that at no time does a valid JSON payload appear in buffer, but instead valid payloads appear along with incomplete payloads. Is it possible to use a regular expression to extract a valid payload from a string? -
why iam run "make" command to install tlwn722n driver, and error at "prandom_u32" [closed]
why iam run "make" command to install tlwn722n driver, and error at "netif_napi_add" cd rtl8188eus make <<ERROR make install -
¿How to config WAF to API GATEWAY/LAMBDA with serverless framework?
I need set a AWS WAF to AWS API GATEWAY with serverless framework. I tried with this plugin (https://www.serverless.com/plugins/serverless-associate-waf) but don't work. I added this code with serverless.yml : plugins: - serverless-associate-waf - serverless-plugin-bind-deployment-id custom: associateWaf: name: Development-waf version: V2 #(optional) Regional | V2 but when I launch deploy, this return an error: Error: The CloudFormation template is invalid: Unresolved resource dependencies [ApiGatewayRestApi] in the Outputs block of the template -
python GIL: Why need Lock when making changes to collections?
Sorry one question, everyone knows Python has GIL, so only one thread is spinning at any one point in time. https://opensource.com/article/17/4/grok-gil This said, then why do we need lock when accessing/modifying collections? Appending or popping elements from a list for example. Or updating a dictionary? Operations like list.sort() also is atomic. I am guessing the answer be: Are you sure any of the operations that you're performing is "Atomic"? If not it can be pre-empted (Preemptive multi-tasking) Thus, in general, because you're unsure how things are implemented under-the-hood, safer to just lock.acquire? Thanks in advance. -
Django Channels AsyncWebsocketConsumer render template
I am trying to send HTML over a websocket via Django channels. It worked fine with a non-asynchronous Websocket Consumer, but upon converting everything to Async I am left with this last error. Here is the problematic code segment and error message: context = { "online_users": redis_instance.smembers(redis_online_name) } await self.channel_layer.group_send( self.room_group_name, { 'type': 'online_users_list', 'html': render_to_string("partials/message/hx_online_users.html", context), } ) server ERROR Exception inside application: 'coroutine' object is not iterable Traceback (most recent call last): File "/.../python3.11/site-packages/channels/routing.py", line 62, in __call__ return await application(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/.../python3.11/site-packages/channels/sessions.py", line 47, in __call__ return await self.inner(dict(scope, cookies=cookies), receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/.../python3.11/site-packages/channels/sessions.py", line 263, in __call__ return await self.inner(wrapper.scope, receive, wrapper.send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/.../python3.11/site-packages/channels/auth.py", line 185, in __call__ return await super().__call__(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/.../python3.11/site-packages/channels/middleware.py", line 24, in __call__ return await self.inner(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/.../python3.11/site-packages/channels/routing.py", line 116, in __call__ return await application( ^^^^^^^^^^^^^^^^^^ File "/.../python3.11/site-packages/channels/consumer.py", line 94, in app return await consumer(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/.../python3.11/site-packages/channels/consumer.py", line 58, in __call__ await await_many_dispatch( File "/.../python3.11/site-packages/channels/utils.py", line 50, in await_many_dispatch await dispatch(result) File "/.../python3.11/site-packages/channels/consumer.py", line 73, in dispatch await handler(message) File "/.../python3.11/site-packages/channels/generic/websocket.py", line 173, in websocket_connect await self.connect() File "/.../chat/consumers.py", line 63, in connect 'html': render_to_string("partials/message/hx_online_users.html", context), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File … -
Writing an asynchronous view function in django for my form
I want to write an async function in django. My view is the same for my GET request and for my POST request (form). My goal is to make my POST asynchronous, so when I send my form, the page does not remain loading and waiting for the server's response. In my POST request I send my form data to an API so I want this function in a coroutine, but it's not working properly. I'm using uvicorn to run my application with the default asgi configuration. I will put my view and my api function here: async def payment_return_view(request): if request.method == 'POST': # do process form form = PaymentReturnForm(request.POST) if form.is_valid(): payment = get_payment_return_api_data(form) # perform request to payment return through API r = await asyncio.gather(*[payment_return(payment)]) request.session[payment.returnID] = r.dict() return redirect("payment_return_result", internal_id=payment.returnID) else: # get params from request params = request.GET payments = get_payments(params).dict() payments = payments.get('entries', []) form = PaymentReturnForm() context = { 'form': form, 'payments': payments, 'params': params } return render(request, 'payment-return.html', context) And this is my async funtion: async def payment_return(payment_return: PaymentReturn) -> Result: d = payment_return.dict() async with aiohttp.ClientSession() as session: async with session.post( f"{API_URL_BASE}/payment/return", data=json.dumps(d, cls=DjangoJSONEncoder), headers={'Content-Type': 'application/json'} ) as r: data = … -
Get the string in an span id tag, wehn running python selenium
I want to extract the word "In stock" from the element. The tag looks like this: <span id="quantitySpan">" in stock" <img class="quantitySpanImg" src="https://d3m9l0v76dty0.cloudfront.net/system/photos/436808/original/34f362ca4ecee94ab2262e159a8ce432.jpg?1588592491"> <-span> I tried to get the string using: driver.find_element(By.ID, "quantitySpan") but it didn't work. I tried by XPath but I'm still not getting the string. driver.find_element(By.XPATH,"/html/body/div[4]/div[2]/div/form/div[2]/div[1]/div[2]/div[2]/span") -
Python, How can I dynamically define a variable when it is imported
I don't actually know if this is possible but I would like to do the following: foo.py: # foo.py my_var = "hello_world" def fancy(s): return s.replace("_"," ").replace("hello", "Hola") main.py: # main.py from foo import my_var, my_var_fancy print(my_var) print(my_var_fancy) And get an output like: >> hello_world >> Hola world Note that I do not want to explicitly define "my_var_fancy" anywhere. I would like I to be dynamically defined when imported using the fancy function. I suspect this could possibly be accomplished with decorators or something similar to a hasattr() check on the module but I'm not sure how. -
Password reset confirm/Password reset request-Django Rest api
I am trying to make reset password functionality.I am able to make the password reset request as I receive the user email for which password need to be changed and successfully send the user the email to reset it(via postman).The problem is when I open the reset mail it directs to the password reset confirm where the user enter the new password to replace the old it in the database.I am unable to handle it with both the GET and POST methods.GET is to display the field where user can enter the new password and POST for posting the request and updating the custom user model. serializers.py from rest_framework import serializers from django.contrib.auth.hashers import make_password from .models import CustomUser class SignUpUserSerialzer(serializers.ModelSerializer): password = serializers.CharField(max_length=100, write_only=True) def validate_password(self, password): return make_password(password) class Meta: model = CustomUser fields = '__all__' class ChangePasswordSerializer(serializers.Serializer): model = CustomUser """ password = serializers.CharField(max_length=100, write_only=True) def validate_password(self, password): return make_password(password) class Meta: model = CustomUser fields = ['password'] urls.py from django.urls import path,include from . import views # from .views import ChangePasswordView urlpatterns = [ path('register/', views.register, name='register'), path('login/', views.custom_login, name='login'), path('logout/', views.custom_logout, name='logout'), path('user/', views.user_profile, name='user_profile'), path('activate/<uidb64>/<token>', views.activate, name='activate'), path("passwordreset/", views.password_reset_request, name="password_reset"), path('reset/<uidb64>/<token>/', views.passwordResetConfirm, name='password_reset_confirm'), ] … -
Django limit records (options) for ModelChoiceField
I have a Model Reader with ModelChoiceField favourite_book which is a Foreign key. It is a DropDown menu and user can choose one from 40.000 possible options (records in the database). There is a problem when editing this Model object and this specific field because DropDown is being populated with all the records from the database which takes too much time and memory. So, I have to find a way to somehow render this field with a matching option (that user has previously selected and saved in DB) alone and not with thousands of other options. I tried: readerForm.fields['books'].initial = Books.objects.get(id=url) but initial doesn't work with bound forms. I have $.ajax request that will later filter those 40.000 options based on input so user can choose some other option if needed. -
Url not found on ajax reqest Django
I want to trigger on update view via ajax call but showing the URL is not found. Any help would be appreciated Ajax: $(document).on("change", "#id_device_type", function() { var id = $(this).val(); $.ajax({ url: '/register-non-device/view/', type: "GET", dataType: 'json', data: $(this).val() }); }); url: path('register-non-device/view/<int:pk>/', RegisterNonDeviceView.as_view(), name='register-non-device') View: class RegisterNonDeviceView(UpdateView): model = AnticipatedNonDevice form_class = AnticipatedNonDeviceForm template_name = 'anticipated_non_device.html' success_url = reverse_lazy('app:po-search-non-device') -
I am trying to calculate the product price before the discount
I am trying to calculate the product price before the discount through the product price and the discount percentage The question is: how do I write this equation price / (1-discount%) into the template in Django Example: <span class="old-price">{{......}} L.E</span> <span class="old-price">{{......}} L.E</span> -
IndexError in the cross_entropy function during training of a neural network,? target 23 is out of bound
IndexError Traceback (most recent call last) <ipython-input-56-7660551ed736> in <module> 162 163 # Call grid search function --> 164 grid_search(train_loader, test_loader, optimizers, learning_rates, activation_fns, hidden_sizes_list, use_batch_norm_list, use_l1_l2_reg_list, use_dropout_list) 4 frames /usr/local/lib/python3.9/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing) 3024 if size_average is not None or reduce is not None: 3025 reduction = _Reduction.legacy_get_string(size_average, reduce) -> 3026 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing) 3027 3028 IndexError: Target 23 is out of bounds. below is my code: # Define function to train the model def train(model, criterion, optimizer, train_loader): start_time = time.time() losses = [] for epoch in range(10): running_loss = 0 for images, labels in train_loader: images, labels = images.to(device), labels.to(device) optimizer.zero_grad() output = model(images) loss = criterion(output, labels) loss.backward() optimizer.step() running_loss += loss.item() losses.append(running_loss/len(train_loader)) print(f"Epoch {epoch+1}, Loss: {running_loss/len(train_loader):.3f}") end_time = time.time() print(f"Training time: {end_time - start_time:.3f} seconds") plt.plot(losses) plt.xlabel('Epoch') plt.ylabel('Loss') plt.show() -
Python "on_destroy" class method
I am wondering if there is a way to call a method before the object is finally "destroyed" - like some sort of event. I am using a class and a static method. Since it is not an instantiated object, I cannot use del and modify __del__ on it. Is the object garbage collected at all if not instantiated? For example: class Test: @staticmethod def say_hello(): print("Hello") def on_destroy(self): print("Object was destroyed...") Test.say_hello() What I want to achieve is that the method on_destroy will fire after say_hello since I am using a static method. I tried using __del__ and it is not being called. Thansk for the help. -
allauth/dj_rest_auth - How come Google OAuth2 works without backend server knowing client_id, client_secret
I tried to implement Google OAuth2 authentication in this project. Backend server runs Django and DRF. To login using rest API, I use dj_rest_auth library. This is roughly how it works: front-end generates auth url like https://accounts.google.com/o/oauth2/auth/i... user authenticates frontend receives access_token access_token is being sent to API to {{API_URL}}auth/google/ backend fetches profile information using this token and saves user I needed to register SocialApp with Google provider but it looks like it is only used for profile fetching. In fact I just used client_id="xxx" and no client_secret. I would expect that the access_token is somehow verified using the client_id and client_secret but obviously it works without them. Is that safe? -
iterate over a set of given URLs and gather the output of data in CSV formate with request and BS4
iterate over a set of URLs and gather the output of data in CSV formate how to iterate over a set of 700 Urls to get the data of 700 digital hubs in CSV (or Excel-formate)- see the page where we have the datasts: shown here https://s3platform.jrc.ec.europa.eu/digital-innovation-hubs-tool with the list of urls like here; https://s3platform-legacy.jrc.ec.europa.eu/digital-innovation-hubs-tool/-/dih/3480/view https://s3platform-legacy.jrc.ec.europa.eu/digital-innovation-hubs-tool/-/dih/13281/view https://s3platform-legacy.jrc.ec.europa.eu/digital-innovation-hubs-tool/-/dih/1417/view https://s3platform-legacy.jrc.ec.europa.eu/digital-innovation-hubs-tool/-/dih/1349/view and so on and so forth question: can we apply this to the similar task too: see the collection of data as digital hubs: i have applied a scraper to a single site with this- and it works - but how to achieve a csv-output to the scraper that iterates on the urls can we put the output into csv too - while applying the same technique!? i want to pair web scraped paragraphs with the most recent scraped heading from the hubCards: I am currently scraping the hubCards as single pages to find the method, however, I would like to get all the 700 Cards scraping scraped with the headings so I can see the data together in a CSV file. i want to write it to results to an appropiate formate - which may be a csv file. Note: we … -
gmaps figure not displaying when using python and vsCode
I'm trying to use python to display a google map. the eventual goal is to put a heatmap overlay of some data on it, but for now I'm just trying to get it to show me a map. here is the code: import gmaps import googlemaps from IPython.display import display # Read the API key from a file with open('api_key.txt') as f: api_key = f.readline().strip() # Create a client object with the API key gmaps_key = googlemaps.Client(key=api_key) # Set the center of the map center = (39.28, -76.61) # Create the map with the center and zoom level fig = gmaps.figure(center=center, zoom_level=13) # Display the map fig display(fig) at first, I was trying to run it from just the python console in vsCode (python 3.11.2), running in windows 10. some googling seemed to indicate that I needed to use a Jupyter notebook, so I did that within vscode and now my python code is running from the notebook (extension: ipynb). I still have no google map being displayed. I read that I may need to enable widgets in in bash/jupyter with jupyter nbextension enable --py widgetsnbextension but that gives an error that jupyter is not installed. I feel like I'm … -
Listening for HTMX events with Alpine.js in Django
Django templates can listen for events from HTMX via listeners added to the Javascript part of code. In this case I want to change DOM element(s) after an update on the page. Below works fine: document.body.addEventListener('htmx:afterSwap', (() => { // get input element, clear its value })) I'm using Alpine.js which helps handling dynamic parts on the client side. I want to connect it to HTMX events since it apparently picks up custom events. Unfortunately, when trying to subscribe to any HTMX event, e.g. htmx:after-swap in <form x-on:htmx:after-swap="console.log('event received')" hx-post="{% url 'my_url' %}" hx-target="#responses" > I have no output in the console. Do I need to first trigger Alpine's 'custom event' from plain Javascript (e.g. in the working addEventListener block above) or can I connect HTMX with Alpine.js directly? I have tried: various cases combinations (I gather kebab is the way for Alpine.js); tried various HTMX events such as htmx:load; replaced x-on with @; found HTMX+Alpine.js examples elsewhere (for instance involving x-data prefix) but none of them worked for me. I would like to understand the process, thank you.